By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,182 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,182 IT Pros & Developers. It's quick & easy.

CSV module, DictReader problem (bug?)

P: n/a
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': ':'}
Nov 1 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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>

Nov 1 '06 #2

P: n/a
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': ':'}
Nov 1 '06 #3

P: n/a
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

Nov 1 '06 #4

P: n/a
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!
Nov 2 '06 #5

P: n/a

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.

Nov 2 '06 #6

P: n/a
>...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.
JohnNot all APIs do the right thing. If you fire up the cmd.exe shell
Johnand feed it slashes as path separators, it barfs. Example:
John C:\junk>dir c:/junk/*.bar
John Invalid switch - "junk".
JohnHence the advice to use rawstrings with backslashes -- they work
Johnunder all circumstances.

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

Skip
Nov 2 '06 #7

P: n/a
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

Nov 2 '06 #8

P: n/a
On 2/11/2006 2:38 PM, sk**@pobox.com 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.

JohnNot all APIs do the right thing. If you fire up the cmd.exe shell
Johnand feed it slashes as path separators, it barfs. Example:
John C:\junk>dir c:/junk/*.bar
John Invalid switch - "junk".
JohnHence the advice to use rawstrings with backslashes -- they work
Johnunder 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

Nov 2 '06 #9

P: n/a
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>

Nov 2 '06 #10

P: n/a
Fredrik Lundh wrote:
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
and as just I pointed out in a comment on that page, getting the slashes
right doesn't help you with spaces in filenames. to write reliable code
for os.system, you want something like:

import os.path
import subprocess

def mysystem(command, *files):
files = map(os.path.normpath, files)
files = subprocess.list2cmdline(files)
return os.system(command + " " + files)

mysystem("more", "/program files/subversion/readme.txt")

but then you might as well use subprocess.call, of course.

</F>

Nov 2 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.