如果它在引号Python中,如何删除字符串中的特定字符

时间:2018-04-12 03:17:34

标签: python regex string

我有一个字符串:

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

如果使用Python在引号之间删除点标记(。),最好的方法是什么? 预期的结果如下:

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

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式(\"[\s\S]*?\")捕获所有引号“...”并在循环中处理替换。首先替换.并存储到newquote。然后用新的引号替换旧引号。

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;