I have some data
'BBB', 175, 115, 67
If I put this data into command like so:
from struct import pack; open("/home/pi/testfile4", "wb").write(pack("BBB", 175, 115, 67))
It runs fine, produces the results I expect.
Now if I put that information into a plain text file using nano and run the following commands:
bin_data = open('/home/pi/file', 'rb').read()
from struct import pack; open("/home/pi/testfile4", "wb").write(pack(bin_data))
I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
struct.error: bad char in struct format
It does not matter if I have 'rb' in the read command for the file. It generates the same error either with or without it.
I am a relative novice when it comes to Python, so I don't know how to do this any other way than the way I am trying. I tried bytearray but it didn't produce the results in the format that I expected.
I have attempted this on a Raspberry Pi with Python 3.7.3 and on a Mac with Python 3.8.2 and they both came up with the same error.
What the heck am I doing wrong, and is there another method that will produce the same results?
And if you are wondering what the heck I am doing, I am converting the data over to a format that the Twinkly Christmas lights will accept. So that three bytes of data are to color a single light with RGB color. A 56 light string requires a 168 byte file for a single frame. I am trying to send 24 frames in the same file, so that is a lot of text, Python does not seem like it wants to deal with that much data in a single command, at least from the interpreter.
Thanks!
1 Answer 1
In the first instance, you are passing pack() the format string BBB, and 3 ints that can each be stored in an unsigned char:
pack("BBB", 175, 115, 67)
In the second instance, you are merely passing pack() one str (the contents read from file, being 'BBB', 175, 115, 67), which it assumes is the format string.
Be sure to read the documentation.
You probably want to do something like this:
from struct import pack
with open("/home/pi/lights.txt") as colours_text_file:
with open("/home/pi/lights.bin", "wb") as colours_bin_file:
for line in colours_text_file:
red, green, blue = map(int, line.rstrip().split(", "))
colours_bin_file.write(pack("BBB", red, green, blue))
Where /home/pi/lights.txt is a text file with contents like:
175, 115, 67
175, 105, 67
175, 95, 67
175, 85, 67
175, 75, 67
175, 65, 67
It reads one line at a line, and transforms the str to 3 ints.
The 3 ints are then packed as unsigned chars and written to /home/pi/lights.bin.