creating a hex value

D

David Bear

I have a file that I need to parse. Items in it are delimited by a hex 15
(0x015). I know it must be trivial to assign a hex value to a variable but
I'm not seeing it in my python essential ref. how can I do

delim = 0x15
while:
ln = file.read()
if ln[0] == delim:
do something

I've looked at the hex function but it doesn't sound like what I want.
 
W

wittempj

What about

martin@lijnbaansgracht:~$ python
Python 2.3.5 (#2, May 4 2005, 08:51:39)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 
J

John Machin

What about

martin@lijnbaansgracht:~$ python
Python 2.3.5 (#2, May 4 2005, 08:51:39)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
4

Quadruple sheeeeeeeeeeeeeesh.
 
F

Fredrik Lundh

David said:
I have a file that I need to parse. Items in it are delimited by a hex 15
(0x015). I know it must be trivial to assign a hex value to a variable but
I'm not seeing it in my python essential ref. how can I do

delim = 0x15
while:
ln = file.read()
if ln[0] == delim:
do something

I've looked at the hex function but it doesn't sound like what I want.

you can use use

ord(ln[0]) == delim

or

ln[0] == '\x15'

or

ln[0] == chr(delim)

or

ln.startswith("\x015")

or some other variation.

fwiw, I'm pretty sure file.read() doesn't do what you want either (unless
you're 100% sure that the file only contains a single item).

if the file isn't larger than a few megs, consider using

items = file.read().split("\x15")

</F>
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top