read from bin file

L

luca72

I have a bin file that i read as:
in_file = open('primo.ske', 'rb')
leggo = luca.readlines()

i get a list like :
['\x00\x80p\x8b\x00\x00\x01\x19\x9b\x11\xa1\xa1\x1f\xc9\x12\xaf\x81!
\x84\x01\x00\x01\x01\x02\xff\xff\x80\x01\x03\xb0\x01\x01\x10m\x7f\n',
etc...]

but if i try to print luca[0]
i get not the the string in the list but i get some like " ò€°‰{"
how i can get a string like the string in the list?
Thanks Luca
 
J

Jerry Hill

I have a bin file that i read as:
in_file = open('primo.ske', 'rb')
leggo = luca.readlines()

i get a list like :
['\x00\x80p\x8b\x00\x00\x01\x19\x9b\x11\xa1\xa1\x1f\xc9\x12\xaf\x81!
\x84\x01\x00\x01\x01\x02\xff\xff\x80\x01\x03\xb0\x01\x01\x10m\x7f\n',
etc...]

but if i try to print  luca[0]
i get not the the string in the list but i get some like " ò€°‰{"
how i can get a string like the string in the list?

print repr(luca[0])
 
D

Dave Angel

Jerry said:
I have a bin file that i read as:
in_file =pen('primo.ske', 'rb')
leggo =uca.readlines()

i get a list like :
['\x00\x80p\x8b\x00\x00\x01\x19\x9b\x11\xa1\xa1\x1f\xc9\x12\xaf\x81!
\x84\x01\x00\x01\x01\x02\xff\xff\x80\x01\x03\xb0\x01\x01\x10m\x7f\n',
etc...]

but if i try to print luca[0]
i get not the the string in the list but i get some like " ò€°‰{"
how i can get a string like the string in the list?

print repr(luca[0])
You have several questions there. First, readlines() doesn't make sense
if the file is binary. Unless you know that \n happens to be a delimiter
in that particular formatted file, it makes more sense to just use
read(), rather than readlines(). And in that case, you get a single
string. You can specify how many bytes you want to read() each time.

Now when you print a list, print calls repr() on each item in the list.
So Jerry is right that repr() is the direct answer to your question.

However, you may want a prettier output, such as that produced by
binascii.hexlify(). A traditional dump file has hex bytes on one side,
and printable characters on the other, with suitable spacing and such.

DaveA
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top