[Csv] PEP 305

Skip Montanaro skip at pobox.com
Sun Oct 17 15:51:39 CEST 2004


    Chris> Ths only thing is that many of the csv files I use have headers -
    Chris> and the Dictionary Reader doesn't cope with them well. 

In 2.4 this gets better.  If the fieldnames arg to the constructor is None,
it assumes the first line contains column headers.

    % head -3 garageband.csv 
    "Bandname","Date","Venue","Address","City","State","Zip","Comments","gig_url","band_url","song_url","venue_url","spotlight"
    "18 Wheeler","2004-10-01","Greenfield's","3355 S Yarrow Street,","Denver","Colorado","","","http://www.garageband.com/gigs/profile.html?|pe1|X8TaC3TQ6KinZ1WxZA","http://www.garageband.com/artist/18wheeler","","http://www.jambase.com/search.asp?venueID=12774&di","F"
    "18 Wheeler","2004-10-02","Greenfield's","3355 S Yarrow Street","Denver","Colorado","","","http://www.garageband.com/gigs/profile.html?|pe1|X8TaC3TQ6KinZ1Wxaw","http://www.garageband.com/artist/18wheeler","","http://www.jambase.com/search.asp?venueID=12774&di","F"
    % python
    Python 2.4a3 (#47, Sep 11 2004, 13:51:15) 
    [GCC 3.3 20030304 (Apple Computer, Inc. build 1493)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import csv
    >>> rdr = csv.DictReader(open("garageband.csv", "rb"))
    >>> rdr.next()
    {'City': 'Denver', 'band_url': 'http://www.garageband.com/artist/18wheeler', 'Zip': '', 'song_url': '', 'Venue': "Greenfield's", 'Comments': '', 'State': 'Colorado', 'venue_url': 'http://www.jambase.com/search.asp?venueID=12774&di', 'Address': '3355 S Yarrow Street,', 'Date': '2004-10-01', 'Bandname': '18 Wheeler', 'spotlight': 'F', 'gig_url': 'http://www.garageband.com/gigs/profile.html?|pe1|X8TaC3TQ6KinZ1WxZA'}
    >>> rdr.next()
    {'City': 'Denver', 'band_url': 'http://www.garageband.com/artist/18wheeler', 'Zip': '', 'song_url': '', 'Venue': "Greenfield's", 'Comments': '', 'State': 'Colorado', 'venue_url': 'http://www.jambase.com/search.asp?venueID=12774&di', 'Address': '3355 S Yarrow Street', 'Date': '2004-10-02', 'Bandname': '18 Wheeler', 'spotlight': 'F', 'gig_url': 'http://www.garageband.com/gigs/profile.html?|pe1|X8TaC3TQ6KinZ1Wxaw'}
    >>> rdr.fieldnames
    ['Bandname', 'Date', 'Venue', 'Address', 'City', 'State', 'Zip', 'Comments', 'gig_url', 'band_url', 'song_url', 'venue_url', 'spotlight']

If you're going to be using 2.2 or 2.3 for awhile, I suggest the following
idiom:

    % python2.3
    Python 2.3.3 (#1, Apr  4 2004, 10:12:27) 
    [GCC 3.3 20030304 (Apple Computer, Inc. build 1493)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import csv
    >>> f = open("garageband.csv", "rb") 
    >>> rdr = csv.reader(f)
    >>> rdr = csv.DictReader(f, rdr.next())
    >>> rdr.next()
    {'City': 'Denver', 'band_url': 'http://www.garageband.com/artist/18wheeler', 'Zip': '', 'song_url': '', 'Venue': "Greenfield's", 'Comments': '', 'State': 'Colorado', 'venue_url': 'http://www.jambase.com/search.asp?venueID=12774&di', 'Address': '3355 S Yarrow Street,', 'Date': '2004-10-01', 'Bandname': '18 Wheeler', 'spotlight': 'F', 'gig_url': 'http://www.garageband.com/gigs/profile.html?|pe1|X8TaC3TQ6KinZ1WxZA'}
    >>> rdr.fieldnames
    ['Bandname', 'Date', 'Venue', 'Address', 'City', 'State', 'Zip', 'Comments', 'gig_url', 'band_url', 'song_url', 'venue_url', 'spotlight']

For writing you still have to explicitly declare the fieldnames to the
constructor, and write the first row:

    fieldnames = [...]
    writer = csv.DictWriter(f, fieldnames)
    writer.writerow(dict(zip(fieldnames, fieldnames)))

Skip


More information about the Csv mailing list