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.
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.
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).
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With