繁体   English   中英

使用 Python 将非结构化文本导入为矩阵

[英]Import an unstructured text as matrices using Python

我需要将非结构化矩阵从文本文件转换为我可以使用的矩阵。 我的文本文件如下所示:

Month 1 - Component 1 

0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 

Month 1 - Component 2 

0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 
0.00 0.00 0.00 0.00 

.
.
.

我有几个月和几个组件,我的目标是将这些数据导入为 arrays 并对所有月份的组件 1 的所有矩阵求和,然后对组件 2 等进行求和。 任何帮助表示赞赏。 谢谢!

如果表的行数始终相同,您可以这样做:

import numpy as np
#tables.txt contains in plain text the columns you posted
tables = np.loadtxt('tables.txt',comments=['\n','M'])
#get number of tables assuming that you always have 4 rows
number_tables = int(tables.shape[0]/4)
#reshape to have number of tablex x rows x columns
tables = np.reshape(tables,(number_tables,4,tables.shape[1]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM