I am parsing a CSV file which is quite large. I am only interested in 2 of the rows (the ones with header Ccy1
and Ccy2
).
So far my approach is to parse the whole file, and any fields that arent in the list of "approved" fields get removed from the list.
I tried this on a small sample file with only 3 rows and it worked fine. When I parsed the real file which has 107 rows more are left than just the "approved" fields.
Why is it not removing all values that are not in the list.
This is my current script:
import csv
data = csv.reader(open('real_sample.csv'))
fields = data.next()
ccy_fields = ['Ccy1', 'Ccy2']
print 'fields: ' + str(fields)
print 'fields to keep: ' + str(ccy_fields)
for item in fields:
if str(item) not in ccy_fields:
fields.remove(item)
print "fields: " + str(fields)