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

csv dictreader

P: n/a
I am trying to use the dictionary reader to import the data from a csv
file and create a dictnary from it but just can't seem to figure it
out.

Here is my code:
>>>import csv
reader = csv.DictReader(open('table.csv'))
for row in reader:
print row
my csv files looks like this:

Bytecode,Element
\x00\x00,0000
\x01\x00,0001
.....
\x09\x00,0009

My output shows:
{'Bytecode': '\\x00\\x00', 'Element': '0000'}
{'Bytecode': '\\x01\\x00', 'Element': '0001'}
....
{'Bytecode': '\\x09\\x00', 'Element': '0009'}

1. how can I get access to this directory
2. why does the values come with two backslashs infront of the "x"
Mar 19 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mar 19, 1:06 pm, brnstrmrs <brnstr...@gmail.comwrote:
I am trying to use the dictionary reader to import the data from a csv
file and create a dictnary from it but just can't seem to figure it
out.

Here is my code:
>>import csv
reader = csv.DictReader(open('table.csv'))
for row in reader:
print row

my csv files looks like this:

Bytecode,Element
\x00\x00,0000
\x01\x00,0001
....
\x09\x00,0009

My output shows:
{'Bytecode': '\\x00\\x00', 'Element': '0000'}
{'Bytecode': '\\x01\\x00', 'Element': '0001'}
...
{'Bytecode': '\\x09\\x00', 'Element': '0009'}

1. how can I get access to this directory
What do you mean? You can get each element in the dict by doing this:

for row in reader:
print row['Bytecode']
print row['Element']

2. why does the values come with two backslashs infront of the "x"
The double back slash is for escaping purposes, I think. If you print
this: '\\x09\\x00'
you'll get this: \x09\x00

Mike
Mar 19 '08 #2

P: n/a
On Mar 19, 2:32 pm, Mike Driscoll <kyoso...@gmail.comwrote:
On Mar 19, 1:06 pm, brnstrmrs <brnstr...@gmail.comwrote:
I am trying to use the dictionary reader to import the data from a csv
file and create a dictnary from it but just can't seem to figure it
out.
Here is my code:
>>>import csv
>>>reader = csv.DictReader(open('table.csv'))
>>>for row in reader:
>>>print row
my csv files looks like this:
Bytecode,Element
\x00\x00,0000
\x01\x00,0001
....
\x09\x00,0009
My output shows:
{'Bytecode': '\\x00\\x00', 'Element': '0000'}
{'Bytecode': '\\x01\\x00', 'Element': '0001'}
...
{'Bytecode': '\\x09\\x00', 'Element': '0009'}
1. how can I get access to this directory

What do you mean? You can get each element in the dict by doing this:

for row in reader:
print row['Bytecode']
print row['Element']
2. why does the values come with two backslashs infront of the "x"

The double back slash is for escaping purposes, I think. If you print
this: '\\x09\\x00'
you'll get this: \x09\x00

Mike
Thanks. It works for the Bytecode but when I do print row['Element']
I get a error message print row['Element'] KeyError: 'Element'
Mar 19 '08 #3

P: n/a
On Mar 19, 1:55 pm, brnstrmrs <brnstr...@gmail.comwrote:
On Mar 19, 2:32 pm, Mike Driscoll <kyoso...@gmail.comwrote:
On Mar 19, 1:06 pm, brnstrmrs <brnstr...@gmail.comwrote:
I am trying to use the dictionary reader to import the data from a csv
file and create a dictnary from it but just can't seem to figure it
out.
Here is my code:
>>import csv
>>reader = csv.DictReader(open('table.csv'))
>>for row in reader:
>>print row
my csv files looks like this:
Bytecode,Element
\x00\x00,0000
\x01\x00,0001
....
\x09\x00,0009
My output shows:
{'Bytecode': '\\x00\\x00', 'Element': '0000'}
{'Bytecode': '\\x01\\x00', 'Element': '0001'}
...
{'Bytecode': '\\x09\\x00', 'Element': '0009'}
1. how can I get access to this directory
What do you mean? You can get each element in the dict by doing this:
for row in reader:
print row['Bytecode']
print row['Element']
2. why does the values come with two backslashs infront of the "x"
The double back slash is for escaping purposes, I think. If you print
this: '\\x09\\x00'
you'll get this: \x09\x00
Mike

Thanks. It works for the Bytecode but when I do print row['Element']
I get a error message print row['Element'] KeyError: 'Element'
That's weird. Since I only had your output, I did the following:

<code>

reader = [ {'Bytecode': '\\x00\\x00', 'Element': '0000'},{'Bytecode':
'\\x01\\x00', 'Element': '0001'} ]

for x in reader:
print x['Element']
print x['Bytecode']
0000
\x00\x00
0001
\x01\x00

</code>

I must be missing something...

Mike
Mar 19 '08 #4

P: n/a
It looks like the backslash is messing the whole thing up so I
reposted to try to find out how to get over the backslash hump.
On Mar 20, 2:08 am, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On Wed, 19 Mar 2008 11:06:40 -0700 (PDT), brnstrmrs
<brnstr...@gmail.comdeclaimed the following in comp.lang.python:
my csv files looks like this:
Bytecode,Element
\x00\x00,0000
\x01\x00,0001
....
\x09\x00,0009

I sure hope your data is more complex than that... otherwise it's a
waste of space...
>>for i in range(10):
... print i, repr(struct.pack("h", i))
...
0 '\x00\x00'
1 '\x01\x00'
2 '\x02\x00'
3 '\x03\x00'
4 '\x04\x00'
5 '\x05\x00'
6 '\x06\x00'
7 '\x07\x00'
8 '\x08\x00'
9 '\t\x00'

And, \t is the same value \x09 (a tab character)
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfr...@ix.netcom.com wulfr...@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-a...@bestiaria.com)
HTTP://www.bestiaria.com/
Mar 20 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.