По какой-то причине этот код работает неправильно. Я пытаюсь заменить только тире, которые не имеют пробелов вокруг них. Тем не менее, тире по-прежнему заменяются, когда нет пробелов.
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 !
Спасибо за помощь!