backslash in reading bytes

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Mar 20 10:33:26 EDT 2008


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



More information about the Python-list mailing list