0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.