我在运行一段程序结束后,将一组数据存到了txt文件,然后需要读取这个文件,获得List。
[34080, 302, 152, 70, 41, 29, 22, 13, 4, 1, 0, 5, 1, 2, 1, 1, 0, 0, 1, 0]
如果直接转List,得到的是字符的List,['[', '3', '4', '0', '8', '0', ',', ' ', '3', '0', '2'.......
with open('F:\\answer_start.txt', 'r') as f:
answer_start = f.read()
answer_start = list(answer_start)
print(answer_start[:50])
用eval()函数:
with open('F:\\answer_start.txt', 'r') as f:
answer_start = f.read()
answer_start = eval(answer_start)
print(answer_start[:50])
得到List