删除在多个标记的单词后找到的连续 symbols/characters
Removing consecutive symbols/characters found after a word for multiple tokens
在不同 words/tokens 之后重复了一个奇怪的图标。示例如下:
到目前为止,我已经使用替换命令将其删除,但是如果对每个单词单独执行,这会变得乏味。
图中所示符号表示为\x9d.
当前python代码如下所示:
import re
text = ['unstable',
'people\x9d.',
'pattern',
'real',
'thought',
'fearful',
'represent',
'contrarians\x9d',
'greedy',
'interesting',
'behaviour',
'opposite']
text = [k.replace('basket\x9d.', 'basket') for k in text]
text = [k.replace('people\x9d.', 'people') for k in text]
text = [k.replace('portfolios.\x9d', 'portfolios') for k in text]
我曾尝试使用 re.sub 检测模式,但未能成功。
text = [re.sub('\x9d', '', str(k)) for k in text]
此代码将完全删除该词。
在这里,您需要删除两个字符的序列,\x9d
和 .
。
您可以在列表理解中使用简单的 str.replace
:
text = [k.replace('\x9d.', '') for k in text]
import re
text = ['unstable','people\x9d.','pattern','real','thought','fearful','represent','contrarians\x9d','greedy','interesting','behaviour','opposite']
text = [k.replace('\x9d.', '') for k in text]
print(text)
# => ['unstable', 'people', 'pattern', 'real', 'thought', 'fearful', 'represent', 'contrarians\x9d', 'greedy', 'interesting', 'behaviour', 'opposite']
在不同 words/tokens 之后重复了一个奇怪的图标。示例如下:
到目前为止,我已经使用替换命令将其删除,但是如果对每个单词单独执行,这会变得乏味。
图中所示符号表示为\x9d.
当前python代码如下所示:
import re
text = ['unstable',
'people\x9d.',
'pattern',
'real',
'thought',
'fearful',
'represent',
'contrarians\x9d',
'greedy',
'interesting',
'behaviour',
'opposite']
text = [k.replace('basket\x9d.', 'basket') for k in text]
text = [k.replace('people\x9d.', 'people') for k in text]
text = [k.replace('portfolios.\x9d', 'portfolios') for k in text]
我曾尝试使用 re.sub 检测模式,但未能成功。
text = [re.sub('\x9d', '', str(k)) for k in text]
此代码将完全删除该词。
在这里,您需要删除两个字符的序列,\x9d
和 .
。
您可以在列表理解中使用简单的 str.replace
:
text = [k.replace('\x9d.', '') for k in text]
import re
text = ['unstable','people\x9d.','pattern','real','thought','fearful','represent','contrarians\x9d','greedy','interesting','behaviour','opposite']
text = [k.replace('\x9d.', '') for k in text]
print(text)
# => ['unstable', 'people', 'pattern', 'real', 'thought', 'fearful', 'represent', 'contrarians\x9d', 'greedy', 'interesting', 'behaviour', 'opposite']