reading files into dicts

Gary Herron gherron at islandtraining.com
Thu Dec 29 19:58:33 EST 2005


rbt wrote:

>What's a good way to write a dictionary out to a file so that it can be 
>easily read back into a dict later? I've used realines() to read text 
>files into lists... how can I do the same thing with dicts? Here's some 
>sample output that I'd like to write to file and then read back into a dict:
>
>{'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994, 
>'.\\New Text Document.txt': 1135900552}
>  
>


A better way, than rolling your own marshaling (as this is called), 
would be to use the cPickle module. It can write almost any Python 
object to a file, and then read it back in later. It's more efficient, 
and way more general than any code you're likely to write yourself.

The contents of the file are quite opaque to anything except the cPickle 
and pickle modules. If you *do* want to roll you own input and output to 
the file, the standard lib functions "repr" and "eval" can be used. Repr 
is meant to write out objects so they can be read back in and recovered 
with eval. If the contents of your dictionary are well behaved enough 
(simple Python objects are, and classes you create may be made so), then 
you may be able to get away with as little as this:

f = file('file.name', 'wb')
f.write(repr(myDictionary))
f.close()

and

f = file('file.name', 'rb')
myDictionary = eval(f.read())
f.close()

Simple as that is, I'd still recommend the cPickle module.

As always, this security warning applys: Evaluating arbitrary text 
allows anyone, who can change that text, to take over complete control 
of your program. So be carefully.

Gary Herron





More information about the Python-list mailing list