Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

import txt file contents to SageMath/Python

I have a text file with contents like the following

[[1, 4], [2, 6], [5, 7]]
[[1, 4], [2, 7], [5, 8]]

I would like to import the contents to SageMath/Python, and assign L=[[[1, 4], [2, 6], [5, 7]], [[1, 4], [2, 7], [5, 8]]].

I tried to use the following

with open('somefile.txt', 'r') as fp: L = [RDF(l.strip()) for l in fp.readlines()]

But there is some error: could not convert string to float: '[[1, 4], [2, 6], [5, 7]]'

How could I solve this problem? How to convert the values to integer (not float)? Thank you very much.

import txt file contents to SageMath/Python

I have a text file with contents like the following

[[1, 4], [2, 6], [5, 7]]
[[1, 4], [2, 7], [5, 8]]

I would like to import the contents to SageMath/Python, and assign L=[[[1, 4], [2, 6], [5, 7]], [[1, 4], [2, 7], [5, 8]]].

I tried to use the following

with open('somefile.txt', 'r') as fp:
    L = [RDF(l.strip()) for l in fp.readlines()]

fp.readlines()]

But there is some error: could not convert string to float: '[[1, 4], [2, 6], [5, 7]]'

How could I solve this problem? How to convert the values to integer (not float)? Thank you very much.