Help with how to combine two csv files

Jean-Michel Pichavant jeanmichel at sequans.com
Wed May 9 06:23:37 EDT 2012


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



More information about the Python-list mailing list