reading files into dicts

Tim Williams (gmail) tdwdotnet at gmail.com
Thu Dec 29 20:01:03 EST 2005


A further thought, if you write the data to a file in the correct
format, you can use import and reload to access the data later instead
of repr.





On 12/29/05, Tim Williams (gmail) <tdwdotnet at gmail.com> wrote:
> On 30/12/05, Giovanni Bajo <noway at sorry.com> wrote:
> >
> >
> > >>> 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}
> >
> > eval() is risky if you can't control the contents of the file  :)
>
>


--

Tim Williams



More information about the Python-list mailing list