converting a dictionary to binary in python
<p>i have a dict of the form : </p>

<pre><code>a = {(7, 190, 0): {0: 0, 1: 10, 2: 10, 3: 37, 4: 45, 5: 49, 6: 69, 7: 45, 8: 130, 9: 59}}
</code></pre>

<p>and am trying to write this to a file in binary format. Currently am using python 2.3</p>

<p>I tried using struct module on a simple list and it looks like it can work but when i move on to the dict, it throws an error saying required argument is not an integer. What will be a good way to tackle this? I tried sth like this:</p>

<pre><code>packed_d=[]
for ssd,(add_val) in a.iteritems(): 
    pack_d=struct.pack('BHB',ssd)
    packed_data.append(pack_d)
</code></pre>

<p>This is where it threw the error..</p>

<p>Any suggestions?</p>

<p>Edited :
cool, thats what i was missing, Janne. 
I tried the following and it looks like its working and was able to unpack as well just to check if it was all ok. Do you think this is a good way to do it? Thanks!</p>

<pre><code>data = {(7, 190, 0): {0: 0, 1: 101, 2: 7, 3: 0, 4: 0}}
packed_data=[]
for ssd,add_val in data.iteritems():
    pack_d=struct.pack('BHB', *ssd)
    packed_data.append(pack_d) 
    for add,val in data[ssd].iteritems():
        pack_t=struct.pack('BH', add,val) 
        packed_data.append(pack_t)
</code></pre>

<p>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']</p>
