Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Comparing specific columns in two csv files

Tags:

python

csv

Say that I have two CSV files (file1 and file2) with contents as shown below:

file1:

fred,43,Male,"23,45",blue,"1, bedrock avenue"

file2:

fred,39,Male,"23,45",blue,"1, bedrock avenue"

I would like to compare these two CSV records to see if columns 0,2,3,4, and 5 are the same. I don't care about column 1.

What's the most pythonic way of doing this?

EDIT:

Some example code would be appreciated.

EDIT2:

Please note the embedded commas need to be handled correctly.

like image 960
coder999 Avatar asked Jan 15 '11 15:01

coder999


People also ask

How do I compare two columns of different CSV files in Python?

Method 2: Compare CSV files using the merge() method It performs an inner join, outer join or both join on columns. You have to just pass the dataframes you want to compare as a list inside the merge() method. The function will compare and returns the dataframe. Run the below lines of code to compare the CSV files.

How do I compare the contents of two CSV files?

Click on "Compare" button to compare your CSV files! You can choose to display only the rows with differences or to display them all (With a color code to visualize the differences).


1 Answers

I suppose the best ways is to use Python library: http://docs.python.org/library/csv.html.

UPDATE (example added):

import csv
reader1 = csv.reader(open('data1.csv', 'rb'), delimiter=',', quotechar='"'))
row1 = reader1.next()
reader2 = csv.reader(open('data2.csv', 'rb'), delimiter=',', quotechar='"'))
row2 = reader2.next()
if (row1[0] == row2[0]) and (row1[2:] == row2[2:]):
    print "eq"
else:
    print "different"
like image 135
Elalfer Avatar answered Nov 09 '22 23:11

Elalfer