read from bin file

Discussion in 'Python' started by luca72, Dec 17, 2009.

  1. luca72

    luca72 Guest

    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
    luca72, Dec 17, 2009
    #1
    1. Advertising

  2. luca72

    Jerry Hill Guest

    On Thu, Dec 17, 2009 at 3:59 PM, luca72 <> 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\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])

    --
    Jerry
    Jerry Hill, Dec 17, 2009
    #2
    1. Advertising

  3. luca72

    Dave Angel Guest

    Jerry Hill wrote:
    > On Thu, Dec 17, 2009 at 3:59 PM, luca72 <> 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\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
    Dave Angel, Dec 17, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Mitchell

    Can "bin" be changed to "cgi-bin" for asp.net

    Kevin Mitchell, Oct 19, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    785
    Wim Hollebrandse
    Oct 19, 2003
  2. John Salerno
    Replies:
    30
    Views:
    1,932
    Stephan Kuhagen
    Aug 10, 2006
  3. sweety
    Replies:
    9
    Views:
    1,002
    Richard Heathfield
    Feb 7, 2006
  4. Yves Dorfsman

    #!/usr/bin/env python vs. #!/usr/bin/python

    Yves Dorfsman, May 2, 2008, in forum: Python
    Replies:
    27
    Views:
    1,966
    Tim Roberts
    May 10, 2008
  5. anne001
    Replies:
    1
    Views:
    391
Loading...

Share This Page