【发布时间】:2016-02-19 17:00:20
【问题描述】:
我正在尝试使用 genfromtxt 从文件中为一组预定义的 x、y 点导入一些数据(压力、应力)。其中数据只是作为由标题名称分割的长列输出,例如:
时间
1.0022181PORE_PRE
-18438721.41
-18438721.41
........STRS_11
-28438721.41
-28438721.41
........
时间数据只有一个点,但PORE_PRE和STRS_11等变量包含很多但数量相等的数据点。我使用以下代码:
import numpy as np
import matplotlib.pyplot as plt
file1=open('Z:/EFNHigh_Res/data_tstep1.out','r')
time=np.genfromtxt(file1,names=None,dtype=None,autostrip=True)
使用此代码,我得到一个结构化数组,其中所有数据都在一列中。通过删除前两行,我设法删除了时间。
我最初的想法是使用与我之前找到的数据点数量和列中数据点总数相关的信息来重塑数组。例如:
xx=np.reshape(time3,307,4)
print xx
但是我得到下面的错误,并且似乎无法找到重塑它的方法,我猜由于数组的一维类型性质,由于某种原因这是不可能的。
File "Z:\EFNHigh_Res\plotting.py", line 39, in <module>
xx=np.reshape(time3,307,4)
File "C:\Python27\ArcGIS10.2\lib\site-packages\numpy\core\fromnumeric.py",line 171, in reshape
return reshape(newshape, order=order)
ValueError: total size of new array must be unchanged
我对输出格式没有太多选择(除了更复杂的安排)。看起来应该是一个简单的操作,但我想不通,但我对python很陌生。 我也尝试使用以下代码仅查看浮点数据,但出现如下错误,或者数据点数量非常多,大于数组中包含的数据点。
xx=time3.view(dtype=np.float)
ValueError: new type not compatible with array
谁能建议我如何处理读取文件?
【问题讨论】:
-
times3的shape和dtype是什么?
标签: python arrays numpy genfromtxt