2016-05-30 10 views
0

私は、ディレクトリ内の複数の.txtファイルからテキストを取り出し、 'Tone analyzer'に対してテキストを実行して音の分析を行うPythonコードを書きたいと考えています。だから私は2つのファイルABC.txtとXYZ.txtがある場合。Pythonコードの出力を持つ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 
ipath = 'C:/TEMP/' # input folder 
opath = 'C:/TEMP/matrix/' # output folder 
reader = codecs.getreader("utf-8") 
tone_analyzer = ToneAnalyzerV3Beta(
    url='https://gateway.watsonplatform.net/tone-analyzer/api', 
    username='1f2fd51b-d0fb-45d8-aba2-08e22777b77d', 
    password='XYXPASS', 
    version='2016-02-11') 
path = 'C:/TEMP/*.txt' 
files = glob.glob(path) 
# iterate over the list getting each file 
for fle in files: 
    # open the file and then call .read() to get the text 
    with open(fle) as f: 
     text = f.read() 
output = f.replace('txt', 'csv') 
output = open(opath + output, mode = 'w') 
data=tone_analyzer.tone(text='text') 
for cat in data['document_tone']['tone_categories']: 
    for tone in cat['tones']: 
     print(tone['tone_name'],tone['score']) 
     #create file 

私は印刷することができる午前:私はこれまでのところ、私が持っているものであるファイルの両方に音解析を行い、ABC.csvとXYZ.csvは、ここでトーン解析の出力を含む2つの出力ファイルを作成するために探していますトーン分析の出力は2つのcsvファイルに別々に保存する方法がわかりません。私は本当にここで何か洞察に感謝します。

+0

ファイルを読み込むたびに、あなたはファイルを書き込むので、何を書くことについての部分を置く場合すべてのファイルのループ内のファイル(最後の数行)?また、画面に印刷するのではなく、実際に出力変数を使用してファイルに書き込むことはしませんか? –

+0

パスワード? ..... – YOU

+0

[ディレクトリからすべてのテキストファイルを読み込む]の可能な複製(http://stackoverflow.com/questions/37534141/reading-all-the-text-files-from-directory) –

答えて

0

は、単にリストにループ出力を保存して、ファイルにリストを書き込むようにcsvモジュールのwriterow()を使用するありがとうございました。以下は、outputがファイル名(拡張子.txtが削除された)の一意の識別子として機能するcsvファイルにtone['tone_name']tone['tone_score']のデータを保存します。

... 
# iterate over the list getting each file 
for fle in files: 
    # 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', '')  
    with open(opath + output + '_tonename.csv', mode = 'w') as csvfile1: 
     writer = csv.writer(csvfile1) 
     for i in tonename: 
      writer.writerow([i]) 

    with open(opath + output + '_tonescore.csv', mode = 'w') as csvfile2: 
     writer = csv.writer(csvfile2) 
     for i in tonescore: 
      writer.writerow([i]) 

あなたは、両方の列を含む1つのcsvファイルが必要な場合は、何のリストはさえ必要ありません:

for fle in files: 
    # 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') 

    # output tone name and score to file 
    output = fle.replace('.txt', '') 
    with open(opath + output + '.csv', mode = 'w') as csvfile: 
     writer = csv.writer(csvfile) 
     writer.writerow(['tone_name','score'])       # HEADERS 
     for cat in data['document_tone']['tone_categories']: 
      for tone in cat['tones']: 
       print(tone['tone_name'],tone['score']) 

       writer.writerow([tone['tone_name'],tone['score']]) # ROWS 
+0

ありがとうございます。このコードを追加しようとすると、次のエラーが発生します。UnicodeDecodeError: 'charmap'コーデックは、位置730のバイト0x8dをデコードできません:文字マップはにマッピングされます。私はあなたの助けを感謝します – Rvs

+0

残念ながら1つの更新は、コードを再度実行しようとしましたが、今度はエラーが発生しています:AttributeError: '_io.TextIOWrapper'オブジェクトに 'replace'属性がありません。私は本当にこの1つの助けに感謝します。 – Rvs

+0

'fle'で' f'を 'output = fle.replace( '。txt'、 '')'に置き換えてください。 – Parfait

関連する問題