I have a .txt file, which is a list of arrays. I want to import that file and extract elements of that list of arrays and work with them. when I use
with open('filename.txt') as f:
list1 = eval(f.read())
which works well when I want to import a list of numbers, does not work when the elements are arrays. It gives
NameError: name 'array' is not defined
Problem is perhaps with the eval() function not working well with arrays? Could you help me import this list of arrays?
CodePudding user response:
If the file content is, for example:
array([1,2,3])
...then...
from numpy import array
with open('filename.txt') as f:
a = eval(f.read())
print(a)
print(type(a))
...will produce...
[1 2 3]
<class 'numpy.ndarray'>
