我正在嘗試在文本文件中存儲語句的正負分數。我想將分數存儲在csv文件中。我已經實現瞭如下代碼:Python - 在CSV文件中存儲浮點數值
import openpyxl
from nltk.tokenize import sent_tokenize
import csv
from senti_classifier import senti_classifier
from nltk.corpus import wordnet
file_content = open('amazon_kindle.txt')
for lines in file_content:
sentence = sent_tokenize(lines)
pos_score,neg_score = senti_classifier.polarity_scores(sentence)
with open('target.csv','w') as f:
writer = csv.writer(f,lineterminator='\n',delimiter=',')
for val in range(pos_score):
writer.writerow(float(s) for s in val[0])
f.close()
但代碼顯示我下面的錯誤在for
循環。
Traceback (most recent call last):
File "C:\Users\pc\AppData\Local\Programs\Python\Python36-32\classifier.py", line 21, in for val in pos_score: TypeError: 'float' object is not iterable
感謝您的幫助 –