Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Python - Reading binary file

  • 13-09-2004 06:49PM
    #1
    Registered Users, Registered Users 2 Posts: 763 ✭✭✭


    This has been wrecking my head for the last hour :)

    I'm trying to read in numerical data from a binary file (lets call it blah) using:
    blah.read(3)
    
    The contents of the file:
    ff 41 ff
    
    The string i get back from read:
    '\xffA\xff'   (converts the 0x41 into ascii character A)
    
    What i want:
    '\xff\x41\xff'
    

    So is there any way of preventing python from converting bytes to ascii in the output string?


Comments

  • Registered Users, Registered Users 2 Posts: 568 ✭✭✭phil


    Data from a file is always a String. You need to look at the ``struct'' module and at the pack/unpack functions.

    Regards,

    Phil.


  • Registered Users, Registered Users 2 Posts: 763 ✭✭✭Dar


    Sorted, thanks!


Advertisement