Manipulating binary data in Python

前端 未结 7 1326
旧时难觅i
旧时难觅i 2021-02-07 07:16

I am opening up a binary file like so:

file = open(\"test/test.x\", \'rb\')

and reading in lines to a list. Each line looks a little like:

7条回答
  •  北海茫月
    2021-02-07 07:45

    Binary data is rarely divided into "lines" separated by '\n'. If it is, it will have an implicit or explicit escape mechanism to distinguish between '\n' as a line terminator and '\n' as part of the data. Reading such a file as lines blindly without knowledge of the escape mechanism is pointless.

    To answer your specific concerns:

    '\x07' is the ASCII BEL character, which was originally for ringing the bell on a teletype machine.

    You can get the integer value of a byte 'b' by doing ord(b).

    HOWEVER, to process binary data properly, you need to know what the layout is. You can have signed and unsigned integers (of sizes 1, 2, 4, 8 bytes), floating point numbers, decimal numbers of varying lengths, fixed length strings, variable length strings, etc etc. Added complication comes from whether the data is recorded in bigendian fashion or littleendian fashion. Once you know all of the above (or have very good informed guesses), the Python struct module should be able to be used for all or most of your processing; the ctypes module may also be useful.

    Does the data format have a name? If so, tell us; we may be able to point you to code or docs.

    You ask "How do I go about using this data safely?" which begs the question: What do you want to use it for? What manipulations do you want to do?

提交回复
热议问题