【发布时间】:2022-01-02 22:26:43
【问题描述】:
如果字符串中还没有特殊字符,我会尝试在特殊字符后添加一个空格。
这是我的代码
import re
line='Hello there! This is Robert. Happy New Year!Are you surprised?Im not.'
for i in re.finditer(r"\?|!|\.", line):
if line[i.end()]!=' ':
line=line.replace(line[i.end()],line[i.end()]+' ')
预期输出:
"Hello there! This is Robert. Happy New Year! Are you surprised? Im not."
我的代码输出:
"Hello t here! This is Robert . Happy New Year!A re you surprised? Im not ."
我还没弄清楚为什么它不起作用。
【问题讨论】:
-
我认为您的代码与结果无关。因为
t被替换为t。所以我怀疑你在真实代码中使用了t。