Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem similar to the one mentioned here:

How to join two CSV files?

I awant to join the file, using the first field of both files as the join key.

The difference is that in one of the two files, the number of columns may vary from one record to another (it contains a traceroute, one ip per column, so the length may change.)

(The join key is the IP address, that is the first column in both files.)

share|improve this question
does the other file have a fixed number of columns? – datatoo May 24 '11 at 15:33
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
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.


file a


file b


Code -

import csv
source1= csv.reader( open("a","rb") )
source2= csv.reader( open("b","rb") )
dest= csv.writer( open("c","wb") )
for row in source1:
for row in source2:
share|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

I would suggest to use SQLite to do this operation. Installing SQLite is very easy, no root permission is required, works well on all platforms.


  1. Import both the CSV files into SQLite DB.
  2. Use SQL to make the join.
  3. Save the output as CSV file.


.separator ","

create table test1 (col1 text, col2 text);

.import test1.csv test1

create table test2 (col1 text, col2 text);

.import test3.csv test1

.mode csv

.output outfile.csv

select a.col1,a.col2,b.col2 from test1 a join test2 b on a.col1=b.col1;

.output stdout

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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