I have a file where some lines are metadata I can ignore and some lines are the printed results of struct.pack calls. Say that f.txt is:
key: 3175
\x00\x00\x00\x00\x00\x00\x00\x00
key: 3266
\x00\x00\x00\x00\x00\x00\x00\x00
In this case, the lines starting with “key” are the metadata and the byte strings are the values I want to extract. Also in this case, the two byte string lines were produced with struct.pack(‘d’, 0). The following code is what I would like to do:
import struct
for line in open('f.txt', 'r'):
# if not metadata, remove newline character and unpack
if line[0:3] != 'key':
val = struct.unpack('d', line[0:-1])
appendToList(val) # do something else with val
With this, I get: “struct.error: unpack requires a string argument of length 8”.
If we modify the code slightly:
import struct
for line in open('f.txt', 'r'):
# if not metadata, remove newline character and unpack
if line[0:3] != 'key': print line[:-1]
then the output is as expected:
\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00
If I put the byte string directly into the unpack call then, I have success:
import struct
# successful unpacking
struct.unpack('d', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
I have tried passing the following variations of line to unpack, all of which give the same result:
str(line)
repr(line)
b"%s" % line
for your string in txt file:
which in python it acturally is:
so you should parse this string and convert it. for your sample, use following code can get what you want: