Quantcast
Channel: Active questions tagged header - Stack Overflow
Viewing all articles
Browse latest Browse all 699

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

$
0
0

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 headerreader.next() # read next line so header will be accessedrfd_header = reader.fieldnamesprint "header:\n"print rfd_header

This results in an error:

AttributeError: '_csv.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.


Viewing all articles
Browse latest Browse all 699

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>