由于某种原因,此代码无法正常工作。我试图只替换周围没有空格的破折号。但是,当没有空格时,破折号仍然会被替换。
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 !
感谢您的帮助!