write to a file two dict()

G

giuseppe.amatulli

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()

but i get the error ValueError: need more than 3 values to unpack.

do you have some suggestions?.
Thanks
Giuseppe
 
M

MRAB

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()

but i get the error ValueError: need more than 3 values to unpack.

do you have some suggestions?.
If they are guaranteed to have the same keys:

a = {1: 1, 2: 2, 3: 3}
b = {1: 11, 2: 22, 3: 33}
for k in a:
output.write("%i %i %i %i\n" % (k, a[k], k, b[k]))

If they don't have the same keys, but are merely the same length, then
you'll first need to decide what it should do.
 
8

88888 Dihedral

(e-mail address removed)æ–¼ 2012å¹´9月24日星期一UTC+8上åˆ1時44分30秒寫é“:
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()



but i get the error ValueError: need more than 3 values to unpack.



do you have some suggestions?.

Thanks

Giuseppe

You can pickle the object directly in python.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top