Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > CSV module, DictReader problem (bug?)

Reply
Thread Tools

CSV module, DictReader problem (bug?)

 
 
Jeff Blaine
Guest
Posts: n/a
 
      11-01-2006
It's been a year or so since I written Python code, so maybe
I am just doing something really dumb, but...

Documentation
=============

class DictReader(csvfile[,fieldnames=None,
[,restkey=None[, restval=None[, dialect='excel'
[, *args, **kwds]]]]])


Create an object which operates like a regular reader
but maps the information read into a dict whose keys
are given by the optional fieldnames parameter. If the
fieldnames parameter is omitted, the values in the
first row of the csvfile will be used as the fieldnames.

Code
====

import csv

r = csv.DictReader('C:\Temp\Book1.csv')
print r.next()
# EOF

Output
======

{'C': ':'}
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-01-2006
Jeff Blaine wrote:

> It's been a year or so since I written Python code, so maybe
> I am just doing something really dumb, but...
>
> Documentation
> =============
>
> class DictReader(csvfile[,fieldnames=None,
> [,restkey=None[, restval=None[, dialect='excel'
> [, *args, **kwds]]]]])
>
>
> Create an object which operates like a regular reader
> but maps the information read into a dict whose keys
> are given by the optional fieldnames parameter. If the
> fieldnames parameter is omitted, the values in the
> first row of the csvfile will be used as the fieldnames.
>
> Code
> ====
>
> import csv
>
> r = csv.DictReader('C:\Temp\Book1.csv')
> print r.next()
> # EOF


here's the documentation for the regular reader, from Python 2.5:

reader(...)
csv_reader = reader(iterable [, dialect='excel']
[optional keyword args])
for row in csv_reader:
process(row)

The "iterable" argument can be any object that returns a line
of input for each iteration, such as a file object or a list.
...

so the reader is simply looping over the characters in your filename. try

r = csv.DictReader(open('C:\Temp\Book1.csv'))

instead.

</F>

 
Reply With Quote
 
 
 
 
Jeff Blaine
Guest
Posts: n/a
 
      11-01-2006
I see what's wrong. Me. Wow am I ever rusty.

Jeff Blaine wrote:
> It's been a year or so since I written Python code, so maybe
> I am just doing something really dumb, but...
>
> Documentation
> =============
>
> class DictReader(csvfile[,fieldnames=None,
> [,restkey=None[, restval=None[, dialect='excel'
> [, *args, **kwds]]]]])
>
>
> Create an object which operates like a regular reader
> but maps the information read into a dict whose keys
> are given by the optional fieldnames parameter. If the
> fieldnames parameter is omitted, the values in the
> first row of the csvfile will be used as the fieldnames.
>
> Code
> ====
>
> import csv
>
> r = csv.DictReader('C:\Temp\Book1.csv')
> print r.next()
> # EOF
>
> Output
> ======
>
> {'C': ':'}

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      11-01-2006
Jeff Blaine wrote:
> It's been a year or so since I written Python code, so maybe
> I am just doing something really dumb, but...
>
> Documentation
> =============
>
> class DictReader(csvfile[,fieldnames=None,
> [,restkey=None[, restval=None[, dialect='excel'
> [, *args, **kwds]]]]])
>
>
> Create an object which operates like a regular reader
> but maps the information read into a dict whose keys
> are given by the optional fieldnames parameter. If the
> fieldnames parameter is omitted, the values in the
> first row of the csvfile will be used as the fieldnames.
>
> Code
> ====
>
> import csv
>
> r = csv.DictReader('C:\Temp\Book1.csv')


Problem 1:

"""csvfile can be any object which supports the iterator protocol and
returns a string each time its next method is called -- file objects
and list objects are both suitable. If csvfile is a file object, it
must be opened with the 'b' flag on platforms where that makes a
difference."""

So, open the file, so that the next() method returns the next chunk of
content, not the next byte in the name of the file.

Note that the arg is called "csvfile", not "csvfilename".

Problem 2: [OK in this instance, but that's like saying you have taken
one step in a minefield and are still alive] backslashes and Windows
file names:

If you were to write 'c:\temp\book1.csv', it would blow up ... because
\t -> tab and \b -> backspace. Get into the habit of *always* using raw
strings r'C:\Temp\Book1.csv' for Windows file names (and re patterns).
You could use double backslashing 'C:\\Temp\\Book1.csv' but it's
uglier.

> print r.next()
> # EOF
>
> Output
> ======
>
> {'C': ':'}


HTH,
John

 
Reply With Quote
 
Tom Plunket
Guest
Posts: n/a
 
      11-02-2006
John Machin wrote:

> If you were to write 'c:\temp\book1.csv', it would blow up ... because
> \t -> tab and \b -> backspace. Get into the habit of *always* using raw
> strings r'C:\Temp\Book1.csv' for Windows file names (and re patterns).
> You could use double backslashing 'C:\\Temp\\Book1.csv' but it's
> uglier.


....alternatively you can just use 'unix slashes', e.g.
'c:/temp/book1.csv', since those work just fine 'cause the Windows
APIs deal with them properly.
-tom!
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      11-02-2006

Tom Plunket wrote:
> John Machin wrote:
>
> > If you were to write 'c:\temp\book1.csv', it would blow up ... because
> > \t -> tab and \b -> backspace. Get into the habit of *always* using raw
> > strings r'C:\Temp\Book1.csv' for Windows file names (and re patterns).
> > You could use double backslashing 'C:\\Temp\\Book1.csv' but it's
> > uglier.

>
> ...alternatively you can just use 'unix slashes', e.g.
> 'c:/temp/book1.csv', since those work just fine 'cause the Windows
> APIs deal with them properly.


Not all APIs do the right thing. If you fire up the cmd.exe shell and
feed it slashes as path separators, it barfs. Example:
C:\junk>dir c:/junk/*.bar
Invalid switch - "junk".
Hence the advice to use rawstrings with backslashes -- they work under
all circumstances.

 
Reply With Quote
 
skip@pobox.com
Guest
Posts: n/a
 
      11-02-2006

>> ...alternatively you can just use 'unix slashes', e.g.
>> 'c:/temp/book1.csv', since those work just fine 'cause the Windows
>> APIs deal with them properly.


John> Not all APIs do the right thing. If you fire up the cmd.exe shell
John> and feed it slashes as path separators, it barfs. Example:
John> C:\junk>dir c:/junk/*.bar
John> Invalid switch - "junk".
John> Hence the advice to use rawstrings with backslashes -- they work
John> under all circumstances.

I think he means "the Windows APIs" within a Python program.

Skip
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      11-02-2006
John Machin wrote:
> Tom Plunket wrote:
>
>>John Machin wrote:
>>
>>
>>>If you were to write 'c:\temp\book1.csv', it would blow up ... because
>>>\t -> tab and \b -> backspace. Get into the habit of *always* using raw
>>>strings r'C:\Temp\Book1.csv' for Windows file names (and re patterns).
>>>You could use double backslashing 'C:\\Temp\\Book1.csv' but it's
>>>uglier.

>>
>>...alternatively you can just use 'unix slashes', e.g.
>>'c:/temp/book1.csv', since those work just fine 'cause the Windows
>>APIs deal with them properly.

>
>
> Not all APIs do the right thing. If you fire up the cmd.exe shell and
> feed it slashes as path separators, it barfs. Example:
> C:\junk>dir c:/junk/*.bar
> Invalid switch - "junk".
> Hence the advice to use rawstrings with backslashes -- they work under
> all circumstances.
>

The command shell is not an API.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      11-02-2006
On 2/11/2006 2:38 PM, wrote:
> >> ...alternatively you can just use 'unix slashes', e.g.
> >> 'c:/temp/book1.csv', since those work just fine 'cause the Windows
> >> APIs deal with them properly.

>
> John> Not all APIs do the right thing. If you fire up the cmd.exe shell
> John> and feed it slashes as path separators, it barfs. Example:
> John> C:\junk>dir c:/junk/*.bar
> John> Invalid switch - "junk".
> John> Hence the advice to use rawstrings with backslashes -- they work
> John> under all circumstances.
>
> I think he means "the Windows APIs" within a Python program.
>
> Skip
>


I too think he meant that. I left the mental gymnastics of wrapping what
I wrote into an os.system call as an exercise for the reader.

| >>> import os
| >>> os.system("dir c:/junk/*.bar")
| Invalid switch - "junk".
| 1
| >>> os.system(r"dir c:\junk\*.bar")
| [snip]
| 02/11/2006 01:21 PM 8 foo.bar
| [snip]
| 0
| >>>

Cheers,
John



 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-02-2006
John Machin wrote:

> Not all APIs do the right thing. If you fire up the cmd.exe shell and
> feed it slashes as path separators, it barfs. Example:
> C:\junk>dir c:/junk/*.bar
> Invalid switch - "junk".
> Hence the advice to use rawstrings with backslashes -- they work under
> all circumstances.


we went through this a couple of days ago; all Windows API:s are
*documented* to accept backward or forward slashes, the command shell is
*documented* to only accept backward slashes.

if you're wrapping some cmd.exe command in an internal API, it's usually
easier to call "os.path.normpath" the last thing you do before you call
"os.system", than to get all the backslashes right in your code.

also see:

http://www.effbot.org/pyfaq/why-can-...-backslash.htm

</F>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
throwing exceptions from csv.DictReader or even csv.reader Tim Python 1 07-05-2010 05:32 PM
Re: using csv dictreader in python Johann Spies Python 0 11-04-2009 10:36 AM
!!!Need Help for csv.DictReader problem!! loveic Python 0 08-26-2009 05:30 PM
csv.DictReader and unicode Laszlo Nagy Python 3 04-07-2008 11:16 AM
csv dictreader brnstrmrs Python 6 03-21-2008 08:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57