I am trying to append two variables tonename and score to the CSV output (code below will only append tone_name) but not sure how to do it, I would really appreciate any help here:
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])
Instead of iterating through tonename you could find the len of tonename and iterate through both tonename & score
for x in range(len(tonename)):
writer.writerow(tonename[x] + ',' + tonescore[x])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.