Ask Your Question

Revision history [back]

One way is to use sage_eval to evaluate a string as Sage code.

Running it on each line gives a list. Then make the list of those lists.

Using the example in the question:

sage: with open('somefile.txt', 'r') as fp:
....:    L = [sage_eval(line) for line in fp.readlines() if line.strip()]
....:
sage: L
[[[1, 4], [2, 6], [5, 7]], [[1, 4], [2, 7], [5, 8]]]

The if line.strip() test is to ignore any empty lines in the file.