Hi, I am fairly new to python and I am doing a project where i need to read a CSV file and compare the columns from each row of a file. If the columns from one row has the same value as the column from another row then write those row's to the same file, if they are different then write them into different files.. so essentially any rows with the same column gets written to the same CSV file.
Here is what I have so far in terms of code
import csv
file = csv.reader(open ('C:/NEW.CSV','r'), delimiter=',')
first = csv.writer(open ('C:/First.CSV','w') )
second = csv.writer(open ('C:/fir.CSV','w'))
header = file.next() #ignore the header
>>> temp = 0
>>> for row in file:
...
... if temp == row[4]: #this is the column i need to compare for each row
... first.writerow( row)
... else:
... second.writerow (row)
...
... temp = row[4]
...
It writes to a CSV file fine but it doesnt write the same rows to the same CSV.
I know the problem is with my assiging temp equal to row[4]. I believe my indentation is wrong, and i tried everything, but when i even tried a simple program for ex:
>>> x = 5
>>> if x ==5:
... print 'x is 5'
... else:
... print 'x is not 5'
...
... print ' this should get printed regardless if x is 5 or not'
Traceback ( File "<interacti ve input>", line 6
print ' this should get printed regardless if x is 5 or not'
^
SyntaxError: invalid syntax
>>>
i get that error. Im not sure how to indent as their are no braces and i would think that my indentation is correct.
Here is what I have so far in terms of code
import csv
file = csv.reader(open ('C:/NEW.CSV','r'), delimiter=',')
first = csv.writer(open ('C:/First.CSV','w') )
second = csv.writer(open ('C:/fir.CSV','w'))
header = file.next() #ignore the header
>>> temp = 0
>>> for row in file:
...
... if temp == row[4]: #this is the column i need to compare for each row
... first.writerow( row)
... else:
... second.writerow (row)
...
... temp = row[4]
...
It writes to a CSV file fine but it doesnt write the same rows to the same CSV.
I know the problem is with my assiging temp equal to row[4]. I believe my indentation is wrong, and i tried everything, but when i even tried a simple program for ex:
>>> x = 5
>>> if x ==5:
... print 'x is 5'
... else:
... print 'x is not 5'
...
... print ' this should get printed regardless if x is 5 or not'
Traceback ( File "<interacti ve input>", line 6
print ' this should get printed regardless if x is 5 or not'
^
SyntaxError: invalid syntax
>>>
i get that error. Im not sure how to indent as their are no braces and i would think that my indentation is correct.
Comment