Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Python - Reading binary file

Options
  • 13-09-2004 6:49pm
    #1
    Registered Users 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 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 Posts: 763 ✭✭✭Dar


    Sorted, thanks!


Advertisement