使用 isspace() 测试索引值的代码输出错误

Wrong output from code using isspace() test over index values

由于某种原因,此代码无法正常工作。我试图只替换周围没有白色 space 的破折号。但是,当没有白色 space.

时,破折号仍会被替换
    ls = []
    for idx, letter in enumerate(line):
        if letter == '-':
            ls.append(idx)
    for m in ls:
        if line[m-1].isspace() == True and line[m+1].isspace() == True:
            line = line[m].replace('-', ' @-@ ')

例如:

If thieves came to you, if robbers by night -- oh, what disaster awaits you -- wouldn't they only steal until they had enough? If grape pickers came to you, wouldn't they leave some gleaning grapes?
How Esau will be ransacked! How his hidden treasures are sought out example-case!

给出:

If thieves came to you , if robbers by night  @-@  @-@  oh , what disaster awaits you  @-@  @-@  wouldn ' t they only steal until they had enough ? If grape pickers came to you , wouldn ' t they leave some gleaning grapes ?
How Esau will be ransacked ! How his hidden treasures are sought out example @-@ case !

注意:这里还有其他数据标记化。

期望的输出是:

If thieves came to you , if robbers by night -- oh , what disaster awaits you -- wouldn ' t they only steal until they had enough ? If grape pickers came to you , wouldn ' t they leave some gleaning grapes ?
How Esau will be ransacked ! How his hidden treasures are sought out example @-@ case !

感谢您的帮助!

你在访问它的时候改变了它,所以如果不手动修复它们,你的索引就会出错。

在这种情况下,您确实需要通过后视来使用正则表达式:

import re

line = "How his hidden treasures -- oh, what was the line again -- are sought out example-case!"
fixed_line = re.sub(r"(?<=[^\s])-(?=[^\s])", " @-@ ", line)
print(fixed_line)

产出

How his hidden treasures -- oh, what was the line again -- are sought out example @-@ case!