python - 如何使用 NLTK 从 CSV 文件中删除停用词?

标签 python csv nltk tokenize data-cleaning

尝试从 3 列的 csv 文件中删除停用词,并使用删除的停用词创建一个新的 csv 文件。但这是成功的,新文件中的数据出现在顶行而不是原始文件中的列。

    import io 
    import codecs
    import csv
    from nltk.corpus import stopwords 
    from nltk.tokenize import word_tokenize 

    stop_words = set(stopwords.words('english')) 
    file1 = codecs.open('soccer.csv','r','utf-8') 
    line = file1.read() 
    words = line.split()
    for r in words: 
        if not r in stop_words: 
            appendFile = open('stopwords_soccer.csv','a', encoding='utf-8') 
            appendFile.write(" "+r)
            appendFile.close()

最佳答案

每写入一行后都需要插入换行符。

for r in words: 
    if not r in stop_words: 
        appendFile = open('stopwords_soccer.csv','a', encoding='utf-8') 
        appendFile.write(r)
        appendFile.write("\n")
        appendFile.close()

这应该可以解决您的问题。

关于python - 如何使用 NLTK 从 CSV 文件中删除停用词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56122854/

相关文章:

python - 使用 python 量化情感分析

python - 如何 prefetch_lated() 与对象相关的所有内容?

python - 如何显示 Pandas 数据框的子集?

python - 来自 tornado.httpclient.AsyncHTTPClient 的 PUT 请求

java - 如何使用 SimpleFlatMapper 将带前缀的 CSV 列别名到映射?

python - 在 Python 中使用 NLTK 的短语索引

python - CondaHTTPError : HTTP 000 CONNECTION FAILED for url <https://conda. anaconda.org/plotly/win-64/current_repodata.json>

mysql - 第 1 行不包含所有列的数据

从 csv 中读取特定的(非连续的)行

python - 在 pandas 中执行 nltk.stem.SnowballStemmer