[Tutor] Working with files, Truckers log

Jeff Shannon jeff@ccvcorp.com
Fri, 05 Apr 2002 13:56:41 -0800


> Alexandre Ratti <alex@gabuzomeu.net> wrote:
>
> >This is a timely subject for me :-). I want to save data that is in a list
> >to a file , to be  used later to append the list.  The list contains decimal
> >numbers.
> >
> >Here is a sample list [ 8.5,8,9.5,10.75,6.25 ]
> >
> >I think I know how to read and write to a file ,but how do I insert this data
> >into the list in my program ?
>
> If you store your values in a text file, for instance one per line, it will
> be fairly simple to load them back and append them to a list. You can use
> eval() to transform a string value into a decimal value.

Why on earth should eval() be used for this, when there's perfectly good conversion
functions??

>>> mylist = ['3.75\n', '8.5\n', '6.0\n']
>>> # to simulate lines that have been
>>> # read in from file.readlines()
>>> [ float(x) for x in mylist ]
[3.75, 8.5, 6.0]
>>>

There really is almost never a reason to use eval().  Really.  :)

Jeff Shannon
Technician/Programmer
Credit International