I'm converting a custom instrument binary data into a CSV file. There're 100 fields with float, int and texts. I'm using bitstruct for it.
with open('instrument.dat','rb') as f:
with open('instrument.csv','w') as w:
header_format = 'u32u8u32t8192'
header_size = calcsize_in_bytes(header_format)
body_format = 'u4u4u1u1u1u1u4u16t96f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32s16s16f32f32u16u16f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f3232f32f32f32f32f32f32f32f32f32f32f32f32s16s16f32f32u16u16f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32<'
body_size = calcsize_in_bytes(body_format)
ver,instrument,date,comment = unpack(header_format, f.read(header_size))
w.write('{},{},{},{}\n'.format(ver,instrument,date,comment))
while True:
field1,...,fieldn = unpack(body_format,f.read(body_size))
My question is how to write body part in CSV? I wouldn't wanna write something cumbersome like:
w.write("{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}...{}\n".format(field1, field2,field3,field4,field5,field6,field7,fiel8,...,fieldN))
Is there any short and smart way to this?
Thanks in advance.
More details:
There are 100 fields with different values in binary. Format is like that:
u4u4u1u1u1u1u4u16t96f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32s16s16f32f32u16u16f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f3232f32f32f32f32f32f32f32f32f32f32f32f32s16s16f32f32u16u16f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32<
u
is unsigned int
, f
is float
, s
is text
are all in bits
All I want is comma-separated values so I can import them into different programs.
Header1,Header2,Header3,…,Header100
Value1,value2,value3,…,value100
...
Value1,value2,value3,...,value100
CodePudding user response:
Final solution, based on above comments.
with open('instrument.dat','rb') as f:
with open('instrument.csv','w') as w:
header_format = 'u32u8u32t8192'
header_size = calcsize_in_bytes(header_format)
body_format = 'u4u4u1u1u1u1u4u16t96f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32s16s16f32f32u16u16f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f3232f32f32f32f32f32f32f32f32f32f32f32f32s16s16f32f32u16u16f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32f32<'
body_size = calcsize_in_bytes(body_format)
ver,instrument,date,comment = unpack(header_format, f.read(header_size))
w.write('{},{},{},{}\n'.format(ver,instrument,date,comment))
while True:
variables = unpack(body_format,f.read(body_size))
brackets = "{}," * len(variables)
brackets = brackes.rstrip(',') "\n"
w.write(brakets.format(*variables))