Help with how to combine two csv files

Sammy Danso samdansobe at yahoo.com
Thu May 10 16:55:40 EDT 2012






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 <jeanmichel at sequans.com> wrote:


From: Jean-Michel Pichavant <jeanmichel at sequans.com>
Subject: Re: Help with how to combine two csv files
To: "Sammy Danso" <samdansobe at yahoo.com>
Cc: python-list at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120510/aec8cd2c/attachment.html>


More information about the Python-list mailing list