Writing dictionary output to a file

Discussion in 'Python' started by dont bother, Mar 6, 2004.

  1. dont bother

    dont bother Guest

    Hey,
    I have a simple problem:
    I have this dictionary output as per the format I
    desired :
    index : value

    However, I want to write this to a file, instead of
    just printing out to the std output.
    I tried to make a new string s= i+ ":" +v
    and fwrite(s) to the file. First it does not work.
    Second I loose the relationship of index with the
    value which I dont want. Can some one point me how to
    write this to a file while still preserving index:
    value relationship as it is present in the current
    dictionary.

    Sorry for the basic questions, I am struggling with a
    project.

    Thx
    Dont
    words = open('dictionary', 'r').read().split()
    dct = {}
    for i in xrange(len(words)):
    dct[words] = i
    #print dct


    for i, v in enumerate(dct):
    #s= i+":"+v
    print i,":",v

    __________________________________
    Do you Yahoo!?
    Yahoo! Search - Find what you’re looking for faster
    http://search.yahoo.com
     
    dont bother, Mar 6, 2004
    #1
    1. Advertising

  2. dont bother

    Peter Otten Guest

    dont bother wrote:

    > I have this dictionary output as per the format I
    > desired :
    > index : value


    > However, I want to write this to a file, instead of
    > just printing out to the std output.


    > for i, v in enumerate(dct):
    > print i,":",v


    Here's how to print to a file instead of stdout:

    dest = file("tmp.txt", "w")
    for i, v in enumerate(dct):
    print >> dest, i, ":", v
    dest.close()

    Peter
     
    Peter Otten, Mar 6, 2004
    #2
    1. Advertising

  3. dont bother

    Ruud de Jong Guest

    dont bother schreef:
    > Hey,
    > I have a simple problem:
    > I have this dictionary output as per the format I
    > desired :
    > index : value
    >
    > However, I want to write this to a file, instead of
    > just printing out to the std output.
    > I tried to make a new string s= i+ ":" +v
    > and fwrite(s) to the file. First it does not work.


    That is because, per your example, i is an integer,
    not a string. s = str(i)+":"+v would work.

    > Second I loose the relationship of index with the
    > value which I dont want. Can some one point me how to
    > write this to a file while still preserving index:
    > value relationship as it is present in the current
    > dictionary.


    Entries in a dictionary are not sorted. But, in
    your example, you already have the index in the
    dictionary. Why don't you use that:

    indexList = [(i,w) for w, i in dct.iteritems()]
    indexList.sort()
    for i, v in indexList:
    s = int(i) + ":" + v
    print s

    Note that here you are basically rebuilding the list
    that you started out with. So, another alternative would
    be to keep that list around, and use that as the source
    for printing:

    for i, w in enumerate(words):
    s = int(i) + ":" + v
    print s

    Regards,

    Ruud.

    >
    > Sorry for the basic questions, I am struggling with a
    > project.
    >
    > Thx
    > Dont
    > words = open('dictionary', 'r').read().split()
    > dct = {}
    > for i in xrange(len(words)):
    > dct[words] = i
    > #print dct
    >
    >
    > for i, v in enumerate(dct):
    > #s= i+":"+v
    > print i,":",v
    >
    > __________________________________
    > Do you Yahoo!?
    > Yahoo! Search - Find what you’re looking for faster
    > http://search.yahoo.com
    >
     
    Ruud de Jong, Mar 6, 2004
    #3
  4. dont bother

    simon place Guest

    i use this to save and reload a dict, slower than pickling but you get a human
    readable file and its soooo simple.


    a={1:'one',2:'two'}
    >>> a

    {1: 'one', 2: 'two'}


    # to save to dict.txt
    >>> print >>file('dict.txt','w+'),str(a)



    # to reload
    >>> exec('b=dict('+file('dict.txt','r').read()+')')



    >>> b

    {1: 'one', 2: 'two'}
    >>>
     
    simon place, Mar 6, 2004
    #4
    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. HNguyen
    Replies:
    4
    Views:
    2,403
    HNguyen
    Dec 21, 2004
  2. dont bother
    Replies:
    2
    Views:
    316
    Ruud de Jong
    Mar 6, 2004
  3. dont bother

    Re: Writing dictionary output to a file

    dont bother, Mar 6, 2004, in forum: Python
    Replies:
    0
    Views:
    753
    dont bother
    Mar 6, 2004
  4. dont bother

    RE: Writing dictionary output to a file

    dont bother, Mar 6, 2004, in forum: Python
    Replies:
    0
    Views:
    383
    dont bother
    Mar 6, 2004
  5. Mav
    Replies:
    22
    Views:
    366
Loading...

Share This Page