将多个值附加到CSV文件

时间:2016-05-31 20:59:50

标签: python csv

我正在尝试将两个变量tonename和得分附加到CSV输出(下面的代码只会附加tone_name)但不知道该怎么做,我真的很感激这里有任何帮助:

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])

1 个答案:

答案 0 :(得分:0)

而不是通过吨位迭代,你可以找到tonename的len并迭代tonename&得分

for x in range(len(tonename)):
  writer.writerow(tonename[x] + ',' + tonescore[x])
相关问题