write to a file two dict()

inq1ltd inq1ltd at inqvista.com
Mon Sep 24 13:39:25 EDT 2012


On Sunday, September 23, 2012 10:44:30 AM giuseppe.amatulli at gmail.com wrote:
> Hi
> Have two dict() of the same length and i want print them to a common file.
> 
> 
> a={1: 1, 2: 2, 3: 3}
> b={1: 11, 2: 22, 3: 33}
> 
> in order to obtain
> 
> 1 1 1 11
> 2 2 2 22
> 3 3 3 33
> 
> I tried
> 
> output = open(dst_file, "w")
> for (a), b , (c) , d in a.items() , b.items() :
>     output.write("%i %i %i %i\n" % (a,b,c,d))
> output.close()

Try this, not exactly what you want, but close.

Refine it to get what you want.

        dictC = {}
        
        a={1: 1, 2: 2, 3: 3}
        b={1: 11, 2: 22, 3: 33}

        for bkey in b.keys():

            newlist = []
            x = str(b.get(bkey, ''))
            x = str(bkey)+x
            newlist.append(x)

            y = str(a.get(bkey, ''))
            y = str(bkey)+y
            newlist.append(y)

            dictC[bkey] = newlist
            
        print dictC, 


jimonlinux,
inqvista.com


> 
> but i get the error ValueError: need more than 3 values to unpack.
> 
> do you have some suggestions?.
> Thanks
> Giuseppe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120924/e8fbdb5f/attachment.html>


More information about the Python-list mailing list