开发者

csvreader.fieldnames not recognized as an attribute of a csv reader object in python

I am trying to extract the header of a CSV file in Python by using the CSV module.

The CSV file is quite flat, and looks something like:

This, That, The Other

1, 2, 3

I am doing the following:

  1. Read in the CSV file and make the reader object
  2. push the reader's iterator to the next line to force it to access the first line at least once (from the csv module 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.")
  3. assigning the .fieldnames attribute to a variable and print it

here is a snippet of code to illustrate:

datafile = open(fname, "rb")
reader = csv.reader(datafile) #use csv module to parse in the header
reader.next() # read next line so header will be accessed
rfd_header = reader.fieldnames

print "header:\n"
print rfd_header

This results in an error:

AttributeError: '_csv.开发者_C百科reader' object has no attribute 'fieldnames'

Which sounds like the .fieldnames attribute isn't there, but is in the documentation of Python 2.6.6 (same version of python I am using)

I would appreciate any insight into this mystery. If there is an alternative method to extract the header that would be awesome too!

Thanks.


If you truly want to use csv.reader instead of csv.DictReader, all you need to do is replace

reader.next() # read next line so header will be accessed
rfd_header = reader.fieldnames

by

rfd_header = reader.next()


Try csv.DictReader instead of csv.reader. The documentation says it too:

DictReader objects have the following public attribute:

csvreader.fieldnames - 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.

http://docs.python.org/library/csv.html


If you need the result in a list, you could take:

rfd_header = reader.next()

This should store the first row (header/fields) to the variable "rfd_header"

Then you could iterate over the variable's values and put into a list

headerList = []
for item in rfd_header:
    headerList.append(item)

Then you can print the result

print headerList


>Apropos of 'rfd_header = reader.next' above, I got this error message in IDLE 3.8.3
>File "<pyshell#2>", line 16, in read_csv_fieldnames
>   fieldnames = csvreader.next()
>AttributeError: '_csv.reader' object has no attribute 'next'
>I fixed it by reading the docco on 'Python.org - CSV File Reading and Writing' 
>Under the subheading 'Reader Objects' on the last line of page 6 it says;
>...Usually you should call this as next(reader)
>1-0 for the Python 'docco'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜