reading files into dicts

Giovanni Bajo noway at sorry.com
Thu Dec 29 19:25:21 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}



>>> d = {'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994,
... '.\\New Text Document.txt': 1135900552}
>>> file("foo", "w").write(repr(d))
>>> data = file("foo").read()
>>> data
"{'.\\\\sync_pics.py': 1135900993, '.\\\\file_history.txt': 1135900994,
'.\\\\New Text Document.txt': 1135900552}"
>>> d = eval(data)
>>> d
{'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994, '.\\New Text
Document.txt': 1135900552}

-- 
Giovanni Bajo





More information about the Python-list mailing list