tags:

views:

158

answers:

2

Trying to read headers for a csv file with:

reader = csv.DictReader(open(PATH_FILE),skipinitialspace=True)
headers = reader.fieldnames

for header in sorted(set(headers)):

It worked on development server, throws this error on production

'NoneType' object is not iterable

Debug shows headers has None value while the csv file has headers in it.

headers:None
+1  A: 

Maybe you're using different Python versions in your development server vs production? In Python 2.5, the fieldnames attribute of a DictReader instance is None until the instance has been used to fetch at least one row.

Alex Martelli
+1  A: 

From csvreader.fieldnames documentation:

If not passed as a parameter when creating the object, this attribute is initialized upon first access or when the first record is read from the file.

So try reading the first row from the file, then reader.fieldnames should contain the data you need. Maybe something like adding reader.next():

reader = csv.DictReader(open(PATH_FILE),skipinitialspace=True)
reader.next()
headers = reader.fieldnames

The documentation also says:

Changed in version 2.6.

So this difference in behaviour could be due to a difference in Python version between your two systems.

Craig McQueen