[英]Open file and create 2D array
I have the file mapData.txt
, which contains several arrays: 我有文件
mapData.txt
,其中包含几个数组:
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
How would I open this file, and create a 2D array using these lines? 我将如何打开此文件,并使用这些行创建2D数组? For example, after creating the 2D array (called, say, "
worldMap
"), if I were to type: 例如,在创建2D数组(称为“
worldMap
”)之后,如果要键入:
print(worldMap[0][0])
It would print 1
它会打印
1
You should add error handling code but this seems to work at least with your example: 您应该添加错误处理代码,但这似乎至少对您的示例有效:
import json
worldMap = []
with open('mapData.txt', 'r') as datafile:
for line in datafile:
worldMap.append(json.loads(line))
print(worldMap[0][0])
As a straightforward, and so efficient approach, you can just use numpy.genfromtxt
: 作为一种简单有效的方法,您可以使用
numpy.genfromtxt
:
import numpy as np
a = np.genfromtxt('a.txt', delimiter=',')
You can use ast.literal_eval
to evaluate each line in your file, or parse it yourself as follows: 您可以使用
ast.literal_eval
评估文件中的每一行,也可以自己解析如下:
>>> def parse_line(line):
... return line.strip('\n[]').split(',')
...
>>> with open('myfile.txt') as f:
... world_map = [list(map(int,parse_line(line))) for line in f]
...
>>> pprint(world_map)
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
>>>
But you should really consider using an existing text-serialization format, like json
. 但是您应该真正考虑使用现有的文本序列化格式,例如
json
。 So, for example: 因此,例如:
>>> my_list = [[1, 2, 3], [4, 5, 6]]
>>> import json
>>> with open('my_list.json', 'w') as f:
... json.dump(my_list, json)
...
So the above writes the json
file to disk. 因此,以上将
json
文件写入磁盘。 Now, to load from disk: 现在,要从磁盘加载:
>>> with open('my_list.json') as f:
... list2 = json.load(f)
...
>>> list2
[[1, 2, 3], [4, 5, 6]]
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.