基本上我有一个文本文件:
-1 2 0
0 0 0
0 2 -1
-1 -2 0
0 -2 2
0 1 0
我想将其放入列表列表中,如下所示:
[[-1,2,0],[0,0,0],[0,2,-1],[-1,-2,0],[0,-2,2],[0,1,0]]
到目前为止我有这段代码,但它会在列表中生成一个字符串列表。
import os
f = open(os.path.expanduser("~/Desktop/example board.txt"))
for line in f:
for i in line:
line = line.strip()
line = line.replace(' ',',')
line = line.replace(',,',',')
print(i)
print(line)
b.append([line])
这产生[['-1,2,0'],['0,0,0'],['0,2,-1'],['-1,-2,0'],['0,-2,2'],['0,1,0']]
了这几乎是我想要的,除了引号。