RE: Writing dictionary output to a file

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

  1. dont bother

    dont bother Guest

    The example on pickle shows how to print number lines
    in a text file:

    http://www.python.org/doc/current/lib/pickle-example.html

    However I saw the usage of pickle module.
    If I use it in my file I get an output like that,

    (dp0
    S'code'
    p1
    I1283
    sS'help'
    p2
    I1021
    sS'precedence'
    p3
    I1010
    sS'text'
    p4
    I972
    sS'both'
    p5
    I1246
    sS'mbqpw'
    p6
    I911
    sS'syntax'
    p7
    I1165
    sS'ratree'

    etc...
    which I reallly dont want.

    My problem still remains unsolved, which I have not
    been able to fix even after trying all keys, values,
    enumerate things associated with dictionary:

    In one simple line:

    How do I associate an index with a dictionary:
    For example:
    My dictionary is this:

    Okay
    Bye
    Take
    Care

    How to I assign an index of 1 with Okay, 2 with Bye, 3
    with Take and 4 with Care.
    If I try dict.keys() It gives me Okay, Bye etc and If
    I try with .values() It gives me some strange numbers


    In other words:
    What I want is:

    When I compare a message with the words in the
    dictionary, whenever there is a match, I want to store
    that index of the dictionary, where there was word and
    compute the value of the corresponding matching word.
    If I use the enumerate, it gives the location of the
    word in the message, which I dont want. I want to
    store the corresponding location of the word in the
    dictionary instead of the corresponding location in
    the message.

    for i in msg:
    if i in dct:
    try:
    vector += 1

    except:
    vector = 1

    for v,i in enumerate(vector):
    vector /= a
    #print v,i, vector ; if u want to see the word
    too that was commmon
    print v,":",vector
    #rint "\n"



    --- David LeBlanc <> wrote:
    >
    > > 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
    > >

    >
    >
    > How about pickling the dictionary? See the pickle
    > module in the doc.
    >
    > import pickle
    >
    > pickle.dump(dct, open('dctfilename.ext', 'w'))
    >
    > Dave LeBlanc
    > Seattle, WA USA
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list



    __________________________________
    Do you Yahoo!?
    Yahoo! Search - Find what you’re looking for faster
    http://search.yahoo.com
     
    dont bother, Mar 6, 2004
    #1
    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,453
    HNguyen
    Dec 21, 2004
  2. dont bother

    Writing dictionary output to a file

    dont bother, Mar 6, 2004, in forum: Python
    Replies:
    3
    Views:
    422
    simon place
    Mar 6, 2004
  3. dont bother
    Replies:
    2
    Views:
    320
    Ruud de Jong
    Mar 6, 2004
  4. dont bother

    Re: Writing dictionary output to a file

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

Share This Page