write to a file two dict()

Discussion in 'Python' started by giuseppe.amatulli@gmail.com, Sep 23, 2012.

  1. Guest

    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
     
    , Sep 23, 2012
    #1
    1. Advertising

  2. MRAB Guest

    On 2012-09-23 18:44, 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.
     
    MRAB, Sep 23, 2012
    #2
    1. Advertising

  3. æ–¼ 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.
     
    88888 Dihedral, Sep 23, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro
    Replies:
    0
    Views:
    429
    Skip Montanaro
    Aug 15, 2003
  2. Alexander Kozlovsky

    dict!ident as equivalent of dict["ident"]

    Alexander Kozlovsky, May 21, 2006, in forum: Python
    Replies:
    5
    Views:
    381
    Alexander Kozlovsky
    May 22, 2006
  3. Paul Melis

    dict.has_key(x) versus 'x in dict'

    Paul Melis, Dec 6, 2006, in forum: Python
    Replies:
    48
    Views:
    1,354
    Kent Johnson
    Dec 15, 2006
  4. Almad
    Replies:
    8
    Views:
    427
    Terry Reedy
    Dec 14, 2006
  5. Drew
    Replies:
    19
    Views:
    1,370
    Duncan Booth
    Mar 15, 2007
Loading...

Share This Page