views:

87

answers:

2

I've just uploaded this CSV file via a form, POSTing it to my Python CGI script. The upload seems to have completed successfully. Permissions on the folder are 777, on the file are 755.

>>> import csv
>>> csvHandle = open('files/TestData.csv', "rb")
>>> csvRawRecordDicts = csv.DictReader(csvHandle)

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: __init__() takes at least 3 arguments (2 given)

The values are as follows:

csvRawRecordDicts undefined, 
csv = <module 'csv' from '/usr/lib/python2.3/csv.pyc'>, 
csv.DictReader = <class csv.DictReader>, 
csvHandle = <open file 'files/TestData.csv', mode 'rb'>

The code works fine on my local computer with Python 2.5. The error happens on 2.3.

What is the thought-process for debugging something like this? Where do I begin looking?

+1  A: 

There are a couple of things you could be doing:

  • read the relevant docs and see that DictReader requires two arguments at least, while you're passing one
  • try to do >>> help(csv.DictReader) and arrive to the same conclusion.

As reading of the docs might explain second of the arguments should be fieldnames (I presume list will suffice), importantly this information was conveyed in the error message!

SilentGhost
I see, I had originally developed the code on Python 2.5 after reading http://docs.python.org/library/csv.html#csv.DictReader which said 'fieldnames' is optional. I see it isn't optional in 2.3! Also thanks for the help() tip.
Pranab
+1  A: 

All of what Silent Ghost said, plus:

A change of emphasis: In my opinion, the most important point is to read the error message carefully. In this case, it is telling you simply and plainly that you need to supply another argument. Checking the help (or the docs for the Python version that you are running) follows ...

Another point: make sure that you understand what your code is doing. csv.DictReader() does NOT open a file. File system permissions are not relevant -- you have already opened the file and passed the handle to csv.DictReader().

John Machin
I'm teaching myself Python, and didn't immediately realise where (or why) any sort of __init__() came into the whole thing.. I would have expected it to plainly say csv.DictReader() requires three arguments. More confusion because it worked fine on 2.5, on my local computer - with all the other issues I'm having, I am beginning to believe it is simpler to port a script from PHP5 to PHP4 than from Python 2.5 to 2.3...
Pranab