【发布时间】:2022-06-30 21:43:09
【问题描述】:
我有这份清单
l1=[[[0,1,2,3,4],[5,6,7]],[8,9,10],[11,12,13,14]]
我将此列表保存在文本文件中
with open('l1.txt', 'w') as f1:
f1.write(str(l1))
现在我有一个包含列表的文本文件。 如何在 python 中阅读此列表? 我试过了
list1= open("l1.txt", "r")
list2= list1.read()
l1= list2.strip('][').split(', ')
有了这个
l1=['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14']
但这不是我一开始的清单
【问题讨论】:
-
l1=[[[0,1,2,3,4][5,6,7]][8,9,10][11,12,13,14]]与TypeError: list indices must be integers or slices, not tuple的 python 不正确,请修正