[英]Removing consecutive symbols/characters found after a word for multiple tokens
在不同的單詞/標記之后重復出現一個奇怪的圖標。 一個例子如下所示:
到目前為止,我已經使用 replace 命令刪除了它,但是如果對每個單詞單獨完成,這可能會變得乏味。
圖像中顯示的符號表示為\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]
請參閱Python 演示:
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']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.