[Tutor] The Evil eval()
Timothy M. Brauch
tbrauch@tbrauch.com
Sat, 13 Apr 2002 00:06:35 -0400
> > I am getting ready to write a program that will read the contents of a
> file
> > and then manipulate the data. The file consists of a single list of
lists
> > all on one line. Something like:
> >
> > [[1, 2, 3, 4, 5], [2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16,
20]]
> >
> > If I understand correctly, when I read the file in, I will get a string
> and
> > to make it into a list of lists, I will have to use eval(). But, I also
> > know that is dangerous and if someone knew I used that for this program,
> > they could really mess
>
> If this list of lists is produced by a Python program (as some of the
> answers
> you got until now seem to assume): why not use pickle?
I wish it were only that easy. I have no idea what language writes the data
files. I think it is java, though. All I get are these small data files
and I have to perform mindless computations on it.
> >>> import pickle
> >>> f = open("pickletest.data","w")
> >>> a = [[1, 2, 3, 4, 5], [2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12,
> 16, 20]]
> >>> pickle.dump(a,f)
> >>> f.close()
> >>> f = open("pickletest.data","r")
> >>> b = pickle.load(f)
> >>> b
> [[1, 2, 3, 4, 5], [2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
> >>>
>
> Wouldn't this solve your problem?
>
> Gregor
I would use pickle as since learning about it, I have learned to love it.
But, I do not have that convience.
- Tim
P.S. My posts are getting through, just a slight four hour delay the last
few days. Hopefully the mailservers of the world look kindly on this
message. I am sending it at 13 April 2002 at 12:06am.