Merge/append CSV files with different headers

V

Vincent Davis

I have several csv file I need to append (vertically). They have different
but overlapping headers. For example;
file1 headers ['a', 'b', 'c']
file2 headers ['d', 'e']
file3 headers ['c', 'd']

Is there a better way than this
import csv
def merge_csv(fileList, newFileName):
allHeaders = set([])
for afile in fileList:
with open(afile, 'rb') as csvfilesin:
eachheader = csv.reader(csvfilesin, delimiter=',').next()
allHeaders.update(eachheader)
print(allHeaders)
with open(newFileName, 'wb') as csvfileout:
outfile = csv.DictWriter(csvfileout, allHeaders)
outfile.writeheader()
for afile in fileList:
print('***'+afile)
with open(afile, 'rb') as csvfilesin:
rows = csv.DictReader(csvfilesin, delimiter=',')
for r in rows:
print(allHeaders.issuperset(r.keys()))
outfile.writerow(r)

Vincent Davis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top