Writing dictionary output to a file

dont bother dontbotherworld at yahoo.com
Sat Mar 6 05:34:16 EST 2004


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[i] += 1

         except:
             vector[i] = 1

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



--- David LeBlanc <whisper at oz.net> 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]] = 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




More information about the Python-list mailing list