你有#线作为基体之间的分离。所以,如果你的文件行,你环线,可以使基质与此#线分开,并建立了他们:
file = open("file.txt", "r")
lines = file.readlines()
在这一点上,线是一个列表。线[i]是线n°i + 1作为一个字符串。
# k, a counter to loop on the lines
k = 1
# Matrix_list is a list of the matrix (size i*j) like that [40*1, 36*1, ...]
Matrix_list = []
while k < len(lines):
if "#" in lines[k-1] and "#" not in lines[k]:
# Start a new matrix
row = []
# Loop to get all the lines of the current matrix
while "#" not in lines[k]:
if k > len(lines):
break
row.appends(lines[k])
k +=1
# At this point, row is a list of every row of your matrix
# First we get the matrix size i*j and create a matrix
i = len(row)
j = len(row[0].split())
Mat = np.zeros((i, j))
row_position = 0
for elt in row:
colum_position = 0
L = elt.split()
for data in L:
Mat[row_position, colum_position] = data
colum_position += 1
row_position +=1
# Once all the data you had was palced in the matrix :
Matrix_list.append(Mat)
else:
k += 1
嗯,我希望你能得到算法的想法,但我很确定它不会马上工作。需要做一些测试和调整,但全球的想法应该做到这一点。最后,你会得到Matrix_list,列表中的每个矩阵都是一个numpy数组。
一旦你有了,你可以做任何你想要的每个矩阵。
你的文件结构如何不是很清楚。你能否详细说明一下? – Eduardo
[为什么“有人可以帮我吗?”不是一个真正的问题?](http://meta.stackoverflow.com/q/284236) – kazemakase
对不起,这里是一个文件的例子:https://www.dropbox的.com/S/uu7u87mthur4thn/files.txt?DL = 0 – Barack