My experience with Python is limited, so I've been using other StackOverflow questions/answers to guide my script, primarily this one: Python: Comparing two CSV files and searching for similar items and this one: How to Delete Rows CSV in python
I am attempting to match IDs from two separate csvs each of which looks roughly like:
ID trip date location
1 1 1/1/2009 384
1 2 1/3/2009 384
1 3 1/7/2009 467
2 1 1/2/2009 842
2 2 1/3/2009 362
I am trying to match based on the trips each ID takes, so for ID 1 in csv1 I look for all IDs in csv2 that have a trip on 1/1/2009 from location 384, then from that list I look for which ones also have a trip on 1/3/2009 from location 384 and so on until there is only one ID from csv2 that matches the ID in csv1: a match. Here is the code I have:
import csv, sys, fileinput
f1 = open('personal2009.csv', 'rb')
f2 = open('company2009.csv', 'rb')
c1 = csv.reader(f1)
c2 = csv.reader(f2)
vmslist = [row for row in c2]
matchDict = {}
ID_list = []
ID = 0
for log_row in c1:
if log_row[0] != ID:
ID = log_row[0]
matchlist = csv.writer(open("matchlist" + str(ID) + ".csv", "wb")
for vms_row in vmslist:
if vms_row[2] == log_row[2] and vms_row[3] == log_row[3]:
matchlist.writerow(vms_row)
elif log_row[0] in ID_list:
continue
else:
f = fileinput.input("matchlist" + str(ID) + ".csv", inplace=True)
w = csv.writer(sys.stdout)
for match_row in csv.reader(f):
if match_row[2] == log_row[2] and match_row[3] == log_row[3]:
w.writerow(row)
if len(list(w)) == 1:
matchDict[str(ID)] = match_row[0]
vessel_list.append(str(ID))
f1.close()
f2.close()
matchlist.close()
Whenever I try to debug or run the code in PythonWin, I get the error Failed to run script - syntax error - invalid syntax in the footer of PythonWin and the cursor stops at the row: for vms_row in vmslist:
I get no further explanation or error information than that. I have tried renaming all my variables and nothing gets past this error, so I have to conclude I am trying to do something incredibly silly and I can't see what it is. Any thoughts?