creating a hex value

Fredrik Lundh fredrik at pythonware.com
Thu Jun 2 06:17:41 EDT 2005


David Bear wrote:

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






More information about the Python-list mailing list