简体   繁体   中英

How to remove specific character in string if it is in quotation mark Python

I have a string:

"Yes, We do. This is a complex issue that deserves more than a one word answer," Zuckerberg said. Pallone called the response "disappointing."

What is the best way to remove dot mark (.) if it in between the quotation marks using Python? the expected result is like:

"Yes, We do This is a complex issue that deserves more than a one word answer," Zuckerberg said. Pallone called the response "disappointing"

Thank you

You can capture all quotes "..." with regex (\\"[\\s\\S]*?\\") and handle the replace in a loop. First replace the . and store into newquote . Then replace the old quote with the new one.

import re;

s = '"Yes, We do. This is a complex issue that deserves more than a one word answer," Zuckerberg said. Pallone called the response "disappointing."';

pattern = re.compile(r'(\"[\s\S]*?\")');

for (quote) in re.findall(pattern, s):
    newquote = quote.replace('.', '');
    s = s.replace(quote, newquote);

print s;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM