Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > read from bin file

Reply
Thread Tools

read from bin file

 
 
luca72
Guest
Posts: n/a
 
      12-17-2009
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\x 01\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
 
Reply With Quote
 
 
 
 
Jerry Hill
Guest
Posts: n/a
 
      12-17-2009
On Thu, Dec 17, 2009 at 3:59 PM, luca72 <(E-Mail Removed)> wrote:
> 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\x 01\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])

--
Jerry
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      12-17-2009


Jerry Hill wrote:
> On Thu, Dec 17, 2009 at 3:59 PM, luca72 <(E-Mail Removed)> wrote:
>
>> 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\x 01\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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
#!/usr/bin/python or #!/usr/bin/env python? John Salerno Python 30 08-10-2006 05:20 AM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
how to encrypt a C data and write a bin file and read a bin at run time and decrypt C data sweety C Programming 9 02-07-2006 05:28 PM
Where do i find the recycle bin? XP PRO with norton protected bin Quiz Time Computer Support 6 06-27-2004 11:13 AM
Can "bin" be changed to "cgi-bin" for asp.net Kevin Mitchell ASP .Net 3 10-19-2003 10:39 PM



Advertisments