def computes(text):
file=open(text)
line=file.readlines()
for i in range(0,len(line)):
print(str(line[i][1]))
输出
[4, 4, 3, 4, 0]
[9, 7, 6, 8, 0]
[6, 6, 4, 2, 2]
[9, 10, 6, 8, 6]
[12, 9, 6, 9, 2]
[12, 12, 2, 4, 1]
[5, 6, 3, 4, 16]
[24, 21, 14, 16, 0]
[3, 2, 2, 5, 0]
[10, 10, 10, 0, 9]
4
9
6
9
1
1
5
2
3
1
我想做的是循环遍历一个文本,里面有一个列表数组,我试图打印数字 12 但只打印第一个数字,我不知道从这里去哪里。我该如何纠正这个问题?
请您参考如下方法:
文件中的数据以文本形式存储,这意味着列表只是字符串表示形式,而不是实际的 Python 对象。所以你需要将它们转换成 Python 列表来打印整个数字。您可以通过执行 ast.literal_eval 来做到这一点在每个列表中:
import ast
def computes(text):
file=open(text)
line=file.readlines()
for i in range(0,len(line)):
print(ast.literal_eval(line[i])[0])
