[英]How do I remove english stop words from a dataframe column using a custom dictionary of stop words
[英]How to remove English Words from a column in a CSV file using Python
Python 非常新。
問題:我有一個 csv 文件,其中包含帶有字母數字文本的行,我想刪除所有英文單詞。 例如,輸入是:“Steam traps on Steam to 56X-233 Butane Vaporizer”,所需的 output 只是:“56X-233”
答案是否類似於使用 NLTK 刪除停用詞?
謝謝你。
如果您不關心匹配實際單詞,您可以使用正則表達式來匹配其中沒有數字的任何單詞:
import re
def remove_words(line):
# Remove words containing only letters
line = re.sub(r"\b[A-Za-z]+\b", "", line)
# Remove remaining extra spaces
return re.sub(" +", " ", line).strip()
print(remove_words("Steam traps on Steam to 56X-233 Butane Vaporizer"))
要對整個文件執行此操作,您只需要獲取文件的每一行並在其上運行上述代碼:
with open("my_file.txt") as f:
for line in f.readlines():
print(remove_words(line))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.