我有一个python代码,它将多个文本文件作为输入并在单独的CSV文件中生成输出,因此如果我的文本文件是ABC.txt和XYX.txt,那么我的代码将生成2个CSV文件中的输出ABC.csv和XYX .csv格式。我的最终目标是获得一个包含所有输出的CSV文件。因为我对sql更熟悉我正考虑将所有文件上传到数据库然后使用sql组合它们但我想知道我是否可以修改下面的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
import xlwt
from bs4 import BeautifulSoup
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='1f2fd51b-d0fb-45d8-aba2-08e22777b77d',
password='DykYfXjV4UXP',
version='2016-02-11')
path = 'C:/TEMP/*.html'
file = glob.glob(path)
# iterate over the list getting each file
writer = csv.writer(open('C:/TEMP/test', mode='w'))
# now enter our input loop
for fle in file:
# open the file and then call .read() to get the text
with open(fle) as f:
...
# output tone name and score to file
for i in tonename:
writer.writerows((tone['tone_name'],tone['score']) for tone in cat['tones'])
答案 0 :(得分:0)
尽可能少地修改现有代码...您只需在输入读取文本文件的循环之前打开csv文件:
...
path = 'C:/TEMP/*.html'
file = glob.glob(path)
# !! open our output csv
writer = csv.writer(open('our-merged-data', mode='w'))
# 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:
...
# output tone name and score to file
for i in tonename:
writer.writerows((tone['tone_name'],tone['score'],Date,Title) for tone in cat['tones'])