2016-05-31 125 views
-1

我想追加兩個變量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]) 

回答

0

而是通過tonename迭代的,你可以找到tonename的LEN,並通過雙方tonename &得分

for x in range(len(tonename)): 
    writer.writerow(tonename[x] + ',' + tonescore[x]) 
+0

謝謝你迭代,我只是改變了代碼(見現在我得到一個錯誤無效語法 – Rvs

+0

編輯哪條線也是指向的它說無效的語法? – CaffeineAddiction

+0

再次感謝,它指向最後一行:writer.writerow(tonename [Y] +','tonescore [Y]) – Rvs