使用python代码输出创建CSV文件

时间:2016-05-30 23:57:45

标签: python csv

我希望编写一个python代码,它将从目录中的多个.txt文件中获取文本,然后针对“音频分析器”运行文本。做分析的基调。所以,如果我有两个文件ABC.txt和XYZ.txt。我期待对文件进行音调分析并创建两个输出文件ABC.csv和XYZ.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

我可以打印音调分析输出,但不知道如何将它们分别保存在两个csv文件中。我真的很感激这里的任何见解。

谢谢

1 个答案:

答案 0 :(得分:0)

只需将循环输出保存到列表,然后使用csv模块的writerow()将列表写入文件。下面会将tone['tone_name']tone['tone_score']数据保存到csv文件中,output作为文件名中的唯一标识符(扩展名.txt已删除)。

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

如果您需要一个包含两个列的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