[Tutor] dictionary

alan.gauld@bt.com alan.gauld@bt.com
Fri, 2 Mar 2001 11:20:35 -0000


> i have file which contain dictionary. and i wrote some code 
> and import the dictionary from that file 

I assume you have a file containing some kind of 
paired information rather than a python dictionary?
Maybe it looks like:
------------
SomeKey: A string that matches the key
AnotherKey: A string that matcher another key
...etc.
------------

You have written a file that reads this file 
- maybe into a Python dictionary? I'll assume 
so...


> and i want to add entry to existing 
> dictionary and after this to save the 
> change to original file.

Add to the dictionary as normal Python:

filedict['BrandNew'] = 'Yet another string'

Then save the entire dictionary by closing the file,
opening it in read mode and writing out your dictionary:

close(infile)
outfile = open('MyDictionaryFile.txt','w')
for key in filedict.getkeys():
   outfile.writeline(key + filedict[key])
close(outfile)

Or am I making lots of wrong assumptions?

Alan g.