[Tutor] The Evil eval()

Sean 'Shaleh' Perry shalehperry@attbi.com
Fri, 12 Apr 2002 00:02:54 -0700 (PDT)


On 11-Apr-2002 Timothy M. Brauch wrote:
> 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 things up.  My question is, how do I make sure that
> what I am eval()-ing is a list of lists (of integers)?  That is the only
> thing the input file should be, and I don't want it to work if there is
> something else inthe input file.
> 

you could use a regex.  The input should only be whitespace, comma, [], or a
number.