0

由于某种原因,此代码无法正常工作。我试图只替换周围没有空格的破折号。但是,当没有空格时,破折号仍然会被替换。

    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 !

感谢您的帮助!

4

1 回答 1

1

您在访问该行时会对其进行变异,因此如果不手动修复它们,您的索引将是错误的。

这确实是您想要使用后向使用正则表达式的情况:

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!
于 2020-02-18T09:00:11.513 回答