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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -