pythonpython-2.7python-bytearray

Python adding cr to lf in binary data?


I was writing a python script which converts an ascii file containing one pair numbers per line to a straight binary representation. Here is my script:

in_file = open("p02_0609.bin", 'r')
out_file = open("sta013.bin", 'w')
out_data = bytearray()

for line in in_file:
    addr, i2c_data = [int(x) for x in line.split(" ")]
    out_data.append(addr)
    out_data.append(i2c_data)

out_file.write(out_data)
out_file.close()
in_file.close()

and a sample of the file it's reading (about 2000 lines total)

58 1
42 4
40 0
41 0
32 0
33 0
34 0
35 0
36 0
37 0
38 0
39 0
40 1
40 2
33 143
40 3
33 0
40 4
40 5
40 6
40 7
40 8
40 9
40 10
40 11

The output file ends on an odd byte, which it shouldn't since all the data is in pairs, and is about 80 bytes longer than expected. After poking around with a hex editor, I finally found the culprit. Every instance of "10" (Ascii LF) has had a CR appended in front of it. How do I make it stop doing that?

Tl;dr: Python is being a dumbass and adding CR to LF in binary data where that makes no sense. How to fix?


Solution

  • You are working with text files so line endings are automatically added by open function. You need to use the mode 'wb' in open for reading and writing bytes.