How to join two CSV files?

same question I want to join 2 files 1st file column 1 2nbd file column 1

except that one of the csv files has variable no of columns in every record it has a traceroute from a certain Ip to a deifferent target everytime the join is based on the target IP it is the 1st column in both csv files

link|improve this question

50% accept rate
does the other file have a fixed number of columns? – datatoo May 24 '11 at 15:33
1  
Can you give us a better description of what you want to accomplish? Your post is poorly formatted and doesn't state what you have and especially, what your desired output is. – slhck May 24 '11 at 16:02
feedback

2 Answers

up vote 0 down vote accepted

Dont know I have read your question right. I would like to use the excellent csv module from python to do this.

Example:

file a

a,x
b,y
c,z

file b

l,m
n,p
k,m

Code - join.py

import csv
source1= csv.reader( open("a","rb") )
source2= csv.reader( open("b","rb") )
dest= csv.writer( open("c","wb") )
for row in source1:
    result=row[1]
    dest.writerow(result)
for row in source2:
    result=row[1]
    dest.writerow(result)
link|improve this answer
I don't think that's what the OP is looking for, I think he wants to join the second columns of each file based on a JOIN of the first columns. – slhck May 24 '11 at 16:01
feedback

I would really use a database for this. You could try using MS Access or OpenOffice Base. You may need to use a spreadsheet (eg Excel) temporarily to help import the CSV files.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.