我有这段在python 2中的代码:
import io
from nltk.corpus import stopwords
import pandas as pd
from Sastrawi.StopWordRemover.StopWordRemoverFactory import StopWordRemoverFactory
factory = StopWordRemoverFactory()
stopwords = factory.get_stop_words()
stop_words = set(stopwords)
file1 = open('sample/wah.csv', 'rb')
line = file1.read()# Use this to read file content as a stream:
words = line.split()
for r in words:
if not r in stop_words:
appendFile = open('sample/wewe.csv','a')
appendFile.write(" "+r)
appendFile.close()
但是结果是相互关联的,如何将其分隔为每一行或每一行?
答案 0 :(得分:1)
import csv as c
with open("C:\\Users\\Yogesh\\Desktop\\csvData.csv") as csv_file:
csv_reader = c.reader(csv_file, delimiter=',')
NumberOfLine = 0
for i in csv_reader:
if NumberOfLine == 0:
print(f'Column names are {", ".join(i)}')
NumberOfLine += 1
else:
print(f'\t{i[0]} , {i[1]} .')
NumberOfLine += 1
print(f'Number of lines {NumberOfLine}')
enter code here
答案 1 :(得分:0)
import io
from nltk.corpus import stopwords
import pandas as pd
from Sastrawi.StopWordRemover.StopWordRemoverFactory import StopWordRemoverFactory
factory = StopWordRemoverFactory()
stopwords = factory.get_stop_words()
stop_words = set(stopwords)
file1 = open('sample/wah.csv', 'rb')
appendFile = open('sample/wewe.csv','a')
for line in file1.readline() # Process each line separately
words = line[:-1].split()
for r in words:
if not r in stop_words:
appendFile.write(r + " ")
appendFile.write("\n")
appendFile.close()
这将逐行写出所有句子。旁注:如果我正确地推断出您想要的内容,则不会创建csv文件。 但是,可以轻松地对此代码进行修改。