Writing dictionary output to a file

D

dont bother

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
 
P

Peter Otten

dont said:
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
 
R

Ruud de Jong

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
 
S

simon place

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'}{1: 'one', 2: 'two'}


# to save to dict.txt

# to reload
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top