Hi JM and All,
Thank you all very much for your response and help. I managed to work out the
problem eventually. But my code is ridiculously long compared to what you have
just offered here. I think your code is elegant and should be much faster.
Thanks a lot
Sammy
Below is my code.
outputfile = codecs.open(csvfile3, 'wb')
inputfile1 = codecs.open(csvfile1, 'rb')
inputfile2 = codecs.open(csvfile2, 'rb')
dictreader2 = csv.DictReader(inputfile2)
dictreader1 = csv.DictReader(inputfile1)
mergedDict = {}
mergedDictb = {}
matchedlistA = []
matchedlistB = []
matchedlist = []
cnt = 0
cntb = 0
for dictline1 in dictreader1:
cnt += 1
print cnt
mergedDict = dictline1.copy()
mergedDict['UniqID']= cnt
matchedlistA.append(mergedDict)
for dictline2 in dictreader2:
cntb += 1
mergedDictb = dictline2.copy()
mergedDictb['UniqID']= cntb
matchedlistB.append(mergedDictb)
for dictline1 in matchedlistA:
for dictline2 in matchedlistB:
if dictline1['UniqID'] == dictline2['UniqID']:
entry = dictline1.copy()
entry.update(dictline2)
matchedlist.append(entry)
--- On Wed, 5/9/12, Jean-Michel Pichavant jeanmic...@sequans.com wrote:
From: Jean-Michel Pichavant jeanmic...@sequans.com
Subject: Re: Help with how to combine two csv files
To: Sammy Danso samdans...@yahoo.com
Cc: python-list@python.org
Date: Wednesday, May 9, 2012, 11:23 AM
Sammy Danso wrote:
Hello Experts,
I am new to python and I have been trying to merge two csv files, and upon
several hours of unsuccessful attempts, I have decided to seek for help.
the format of the file is as follows. file A has columns a, b, c and values
1,2,3 for several rows. File B also has columns d,e and values 4,5 for same
number of rows as A. the files however do not have any unique column between
the two.
I want an output file C to have columns a,b,c,d,e with values 1,2,3,4,5
I would be very grateful for your help with some code.
Thanks very much,
Sammy
Post some code so we may point at your problem.
The solution is easy, the code very small.
Something like (pseudo code):
import csv
writer = csv.writer(file3)
for index, row in enumerate(csv.reader(file1)):
writer.writerow(row + csv.reader(file2)[index])
JM
--
http://mail.python.org/mailman/listinfo/python-list