May-19-2019, 07:31 AM
Your code is OK except "B" for "!H".
And you need an editor which is able to open
the file as UTF-16 with BOM.
Or without struct:
And you need an editor which is able to open
the file as UTF-16 with BOM.
#!/usr/bin/python3 import struct lst = ('11111111', '11111110', '01101000', '00000000', '01101001', '00000000', '00001101', '00000000', '00001010', '00000000') def writer(stuff): blob = struct.pack('B', int(stuff, 2)) with open('test.txt', "a+b") as file: file.write(blob) for strings in lst: writer(strings) #done
Or without struct:
#!/usr/bin/python3 lst = ('11111111', '11111110', '01101000', '00000000', '01101001', '00000000', '00001101', '00000000', '00001010', '00000000') def writer(stuff): blob = int(stuff, 2).to_bytes(1, byteorder='big') with open('test.txt', "a+b") as file: file.write(blob) for strings in lst: writer(strings) #done