Read in binary mode interpreting newline character
Question: I have a binary file that I am trying to parse based on a protocol. In the
binary data there is this value `'\nW\xe1A'` which was generated by the C
struct like so:

    
    
    struct. pack( '< f',28.1675).
    

When I try to open the file with `'rb'` and use `unpack` to get the float
back, the read(length) seems only getting the part without `'\nW'`, just
`'\xe1A'`, it is like Python is interpreting the newline character `'\n'`, but
open with `'rb'` is supposed to avoid this ?

Is there any solution to this?


Answer: There should be nothing wrong with the code you show. Here is a minimal
example:

    
    
    import struct
    
    s = struct.pack("<f", 28.1675)
    open("/tmp/testi.dat", "wb").write(s)
    
    s1 = open("/tmp/testi.dat", "rb").read()
    print struct.unpack("<f", s1)[0]
    

This gives 28.1674995422, which is pretty close. If you use both "rb" and
"wb", there should be no newline hassle in the way.

