converting a dictionary to binary in python -
i have dict of form :
a = {(7, 190, 0): {0: 0, 1: 10, 2: 10, 3: 37, 4: 45, 5: 49, 6: 69, 7: 45, 8: 130, 9: 59}}
and trying write file in binary format. using python 2.3
i tried using struct module on simple list , looks can work when move on dict, throws error saying required argument not integer. way tackle this? tried sth this:
packed_d=[] ssd,(add_val) in a.iteritems(): pack_d=struct.pack('bhb',ssd) packed_data.append(pack_d)
this threw error..
any suggestions?
edited : cool, thats missing, janne. tried following , looks working , able unpack check if ok. think way it? thanks!
data = {(7, 190, 0): {0: 0, 1: 101, 2: 7, 3: 0, 4: 0}} packed_data=[] ssd,add_val in data.iteritems(): pack_d=struct.pack('bhb', *ssd) packed_data.append(pack_d) add,val in data[ssd].iteritems(): pack_t=struct.pack('bh', add,val) packed_data.append(pack_t)
packed_data = ['\x07\x00\xbe\x00\x00', '\x00\x00\x00\x00', '\x01\x00e\x00', '\x02\x00\x07\x00', '\x03\x00\x00\x00', '\x04\x00\x00\x00']
ssd
tuple. unpack individual arguments adding asterisk in front:
struct.pack('bhb', *ssd)
Comments
Post a Comment