追加到CSV多个值文件
问题描述:
我想追加两个变量tonename和得分的CSV输出(下面的代码将只追加tone_name),但不知道该怎么做,我会很感激这里的任何帮助:追加到CSV多个值文件
for fle in file:
# open the file and then call .read() to get the text
with open(fle) as f:
text = f.read
# tone analysis
data=tone_analyzer.tone(text='text')
# iterate through tone analysis data
tonename=[]; tonescore=[]
for cat in data['document_tone']['tone_categories']:
for tone in cat['tones']:
tonename.append(tone['tone_name'])
tonescore.append(tone['score'])
print(tone['tone_name'],tone['score'])
# output tone name and score to file
output = fle.replace('.txt', '')
X=output
with open(X+'_tonename.csv', mode = 'w') as csvfile1:
writer = csv.writer(csvfile1)
for Y in range(len(tonename)):
writer.writerow(tonename[Y] + ',' tonescore[Y])
答
而是通过tonename迭代的,你可以找到tonename的LEN,并通过双方tonename &得分
for x in range(len(tonename)):
writer.writerow(tonename[x] + ',' + tonescore[x])
谢谢你迭代,我只是改变了代码(见现在我得到一个错误无效语法 – Rvs
编辑哪条线也是指向的它说无效的语法? – CaffeineAddiction
再次感谢,它指向最后一行:writer.writerow(tonename [Y] +','tonescore [Y]) – Rvs