I try to compare two csv files. First file (movements.csv) has 14 columns, second csv (LCC.csv) one single column. I want to check whether the entries (strings) of column 8 in movements.csv appear somewhere in column 1 of LCC.csv. If so, in column 14 a 'Yes' should be written, if not a 'No'. The code I tried so far is and the error message I receive:
import csv
f1 = file('LCC.csv', 'rb')
f2 = file('movements.csv', 'rb')
f3 = ('output.csv', 'wb')
c1 = csv.reader(f1)
c2 = csv.reader(f2)
c3 = csv.writer(f3)
movements = list(c2)
for LCC_row in c1:
row = 0
found = False
for movements_row in movements:
output_row = movements_row
if movements_row[7] == LCC_row[0]
output_row.append('Yes')
found = True
break
row += 1
if not found:
output_row.append('No')
c3.writerow(output_row)
f1.close()
f2.close()
f3.close()
I'm a complete beginner with python, so any advice is appreciated! Optimally the check between the two columns would also disregard whether the strings are written in capital letters or not.
The error message comes after
c3.writerow(output_row)
as
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
>>>
LCC.csv (no header):
Air Ab
Jamb
Sw
AIRF
EURO
movements.csv (has a header):
ap,ic,year,y_m,pas,da,ty,airl,ic_a,dest_orig,ic_d,coun,cont,LCC
Zue,LSZH,2005,200501,25,1/1/2005,Dep,"EURO",EUJ,"Mans C",EG,Gb,Eu,
Zue,LSZH,2005,200501,204,1/1/2005,Arr,"Sw",SWR,"Dar",HA,Tans,A,
Ba,LSZM,2005,200501,191,1/1/2005,Arr,"AIRF",AFR,"PG",LG,Fr,Eu,
Zue,LSZH,2005,200501,228,1/1/2005,Dep,"THA",THA,Bang,VD,Th,As,
as already said, the last column (LCC) is completely empty at the moment