Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29830 > unrolled thread

write to a file two dict()

Started bygiuseppe.amatulli@gmail.com
First post2012-09-23 10:44 -0700
Last post2012-09-23 13:34 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  write to a file two dict() giuseppe.amatulli@gmail.com - 2012-09-23 10:44 -0700
    Re: write to a file two dict() MRAB <python@mrabarnett.plus.com> - 2012-09-23 19:00 +0100
    Re: write to a file two dict() 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-23 13:34 -0700

#29830 — write to a file two dict()

Fromgiuseppe.amatulli@gmail.com
Date2012-09-23 10:44 -0700
Subjectwrite to a file two dict()
Message-ID<d0910f3d-5c9d-4c0f-8f3d-20ac394c2f45@googlegroups.com>
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

[toc] | [next] | [standalone]


#29838

FromMRAB <python@mrabarnett.plus.com>
Date2012-09-23 19:00 +0100
Message-ID<mailman.1138.1348423418.27098.python-list@python.org>
In reply to#29830
On 2012-09-23 18:44, giuseppe.amatulli@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()
>
> 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.

[toc] | [prev] | [next] | [standalone]


#29855

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-09-23 13:34 -0700
Message-ID<dcc6f3ef-d077-47b3-a829-0c09bf6d7a93@googlegroups.com>
In reply to#29830
giuseppe...@gmail.com於 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. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web