如何以这种方式显示CSV中的200行数据“”行[0]。拆分(“;”)

2024-10-02 18:23:08 发布

您现在位置:Python中文网/ 问答频道 /正文

如何显示CSV中的200行数据?我使用了line[0].split(';'),但它只显示一行数据

Test_X = []
with open('data testing 2.csv', 'r', encoding="utf-8") as f:
    reader = csv.reader(f, delimiter='\t')
    for i, line in enumerate(reader):
        tweet = line[0].split(';')

cleaning2 = cleaning(Test_X)
stemming2 = stemming(cleaning2)
tokenizing2 = tokenizing(stemming2)
stopwordremoval2 = stopwordremoval(tokenizing2)
fit_sw2 = fit_sw(stopwordremoval2)
count_vect2 = count_vect.transform([fit_sw2])
tf_idf2 = tf_idf.transform(count_vect2)

hasilpred_svm = model_svm.predict(tf_idf2)
with open('Hasil Testing SVM.csv', 'a', newline='') as f:
    writer = csv.writer(f)
    writer.writerow([fit_sw2, hasilpred_svm])

hasilpred_nb = model_nb.predict(tf_idf2)
with open('Hasil Testing NB.csv', 'a', newline='') as f:
    writer = csv.writer(f)
    writer.writerow([fit_sw2, hasilpred_nb])

这是输出:

output

CSV文件:

CSV file


Tags: csvtfascountwithlineopenreader
1条回答
网友
1楼 · 发布于 2024-10-02 18:23:08

您应该首先读取文件的所有行。像

with open('data testing 2.csv', 'r') as read_obj:
    # pass the file object to reader() to get the reader object
    csv_reader = reader(read_obj)
    # Iterate over each row in the csv using reader object
    for row in csv_reader:
        # row variable is a list that represents a row in csv
        print(row)

相关问题 更多 >