0
これは私が既に尋ねた質問のように聞こえるかもしれませんが、テキストアナライザーでtext/htmlファイルを実行してCSVファイルに出力するPythonコードがあります。私が直面している問題は、出力 'Tone_name'から2つの変数を作成し、同じCSVファイルの2つの列として格納する必要があることです。ここで私はこれまで(1つの変数のために完璧に働いて)しているコードは次のとおりです。複数の変数をCSVファイルに追加する
import json
from watson_developer_cloud import ToneAnalyzerV3Beta
import urllib.request
import codecs
import csv
import os
import re
import sys
import collections
import glob
import xlwt
ipath = 'C:/TEMP/' # input folder
opath = 'C:/TEMP/' # output folder
reader = codecs.getreader("utf-8")
tone_analyzer = ToneAnalyzerV3Beta(
url='https://gateway.watsonplatform.net/tone-analyzer/api',
username='abcid',
password='pass',
version='2016-02-11')
path = 'C:/TEMP/*.txt'
file = glob.glob(path)
# iterate over the list getting each file
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']:1
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 i in tonename:
writer.writerow([i])
は、私が「tone_name」と一緒に列としてスコアを追加することができます方法はありますか?
は私が変更を行ったが、今、私はこのエラーを取得しています、ありがとうございます。私は変更されたコードでコードを編集しました。 – ravsd
私の悪いことはもう一度それを試して、それは完全に働いた。手伝ってくれてどうもありがとう。 – ravsd