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

backslash in reading bytes

P: n/a
I am trying to read some byte data as a string then using a library to
convert them a code:
>>>reader = csv.DictReader(open('table.txt'))
def eleFind(value):
for row in reader:
if row['byteCode'] == value:
print row['Element']
return
else:
print "No Match Found:"
eleFind('\x00\x00')
My table contains:

\x00\x00,0000
\x01\x00,0000
.......

The program errors out. How can I fix/overide this backslash issue.
Mar 20 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Thu, 20 Mar 2008 03:24:49 -0700, Dravidan wrote:
I am trying to read some byte data as a string then using a library to
convert them a code:
>>>>reader = csv.DictReader(open('table.txt'))
def eleFind(value):
for row in reader:
if row['byteCode'] == value:
print row['Element']
return
else:
print "No Match Found:"
eleFind('\x00\x00')

My table contains:

\x00\x00,0000
\x01\x00,0000
......

The program errors out. How can I fix/overide this backslash issue.
What does `errors out` mean?

It won't find two zero bytes. What you give at the `eleFind()` call are
just *two* characters with a byte value of zero:

In [116]: len('\x00\x00')
Out[116]: 2

In [117]: print '\x00\x00'
In [118]: len('\\x00\\x00')
Out[118]: 8

In [119]: print '\\x00\\x00'
\x00\x00

The backslash has a special meaning in string literals. If you don't want
this meaning, you have to escape it with another backslash.

Ciao,
Marc 'BlackJack' Rintsch
Mar 20 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.