I have the problem to understand how to import an created and existing array from the file.txt which has just that array in it and save it in the new .py file under an new name to work with it.
the file.txt has following content:
[['name1', 'x', '1', 'a'], ['name2', 'y', '2', 'b'], ['name3', 'b', '3', 'c'], ['name4', '1', '44', '12']]
i tried somethings like that:
text_file = open("file.txt", "r")
lines = text_file.readlines()
print(lines)
text_file.close()
type is list but i have the same list just with new [""] and same content
print(type(lines))
["[['name1', 'x', '1', 'a'], ['name2', 'y', '2', 'b'], ['name3', 'b', '3', 'c'], ['name4', '1', '44', '12']]"]
and if i try to read the array with
print(lines[1])
or
print(lines[1][2])
i get an error. thx for any help
Update: The answer is:
import ast
text_file = open("file.txt", "r")
line = text_file.readlines();
lines = eval(line[0])
text_file.close()