如果使用numpy数组,应该考虑使用
numpy.savetxt()
改为函数
https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.savetxt.html
. 例如:
import numpy as np
a = np.random.randint(0, 10, (10, 10), dtype=int)
a[1:5, 1:8] = 0
np.savetxt('1.txt', a, fmt='%d', delimiter=',')
文件内容:
0,8,5,8,0,7,5,8,0,9
0,0,0,0,0,0,0,0,3,4
5,0,0,0,0,0,0,0,7,3
9,0,0,0,0,0,0,0,7,5
7,0,0,0,0,0,0,0,6,9
9,9,9,9,2,7,5,0,0,7
4,6,9,0,7,5,2,4,7,5
2,5,1,9,4,9,3,5,3,7
3,3,6,8,5,7,5,8,5,5
9,4,1,2,0,9,2,2,8,2
numpy.loadtxt()
https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html
:
a = np.loadtxt('1.txt', delimiter=',', dtype=int)
那么
a
是:
array([[0, 8, 5, 8, 0, 7, 5, 8, 0, 9],
[0, 0, 0, 0, 0, 0, 0, 0, 3, 4],
[5, 0, 0, 0, 0, 0, 0, 0, 7, 3],
[9, 0, 0, 0, 0, 0, 0, 0, 7, 5],
[7, 0, 0, 0, 0, 0, 0, 0, 6, 9],
[9, 9, 9, 9, 2, 7, 5, 0, 0, 7],
[4, 6, 9, 0, 7, 5, 2, 4, 7, 5],
[2, 5, 1, 9, 4, 9, 3, 5, 3, 7],
[3, 3, 6, 8, 5, 7, 5, 8, 5, 5],
[9, 4, 1, 2, 0, 9, 2, 2, 8, 2]])