Reading CSV files. I want to raise error message if there is no any header from following list. It must be atleast one header in csv file.
Headers are
age sex city. I'm trying like this. thanks
with open('data.csv') as f:
cf = csv.DictReader(f, fieldnames=['city'])
for row in cf:
print row['city']
How about this?
import csv
with open('data.csv', 'rb') as inf:
cf = csv.reader(inf)
header = cf.next()
if header != ['Age', 'Sex', 'City']:
print "No header found"
else:
for row in cf:
print row[2]
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