backslash in reading bytes

D

Dravidan

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.
 
M

Marc 'BlackJack' Rintsch

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top