Advise of programming one of my first programs

Tim Chase python.list at tim.thechases.com
Tue Mar 27 11:53:11 EDT 2012


On 03/27/12 10:32, Prasad, Ramit wrote:
>> fileread = open('myfile.txt','r')
>> tbook = eval(fileread.read())
>> fileread.close()
>
> The use of eval is dangerous if you are not *completely* sure what is
> being passed in. Try using pickle instead:
> http://docs.python.org/release/2.5.2/lib/pickle-example.html

Or, depending on the use, you might use ast.literal_eval()

A cursory glance at the OP's code suggests that this may simply 
be a dict of values-to-lists of purely literals, so 
literal_eval() should do the job.

-tkc





More information about the Python-list mailing list