简体   繁体   中英

Combining multiple CSV files into 1

I have a python code that takes multiple text files as input and generates output in separate CSV file so if my text files are ABC.txt and XYX.txt then my code is generating output in 2 CSV files ABC.csv and XYX.csv. My ultimate goal is get one single CSV file with all the outputs. Since I am more comfortable with sql I was thinking about uploading all the files to a database and then combine them using sql but I was wondering if I can modify my python code below to generate one single CSV file containing all output. Here is my code:

 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'])

Modifying your existing code as little as possible ... you simply need to open the csv file before entering your loop that reads the text files:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM