我正在尝试将.dat文件中的数据读入数据帧。但是格式和我通常看到的有点不同。(这是一个裁剪视图,每个字段有更多的行,并且有更多的字段)
#date=2017102600
#minlen=000
#maxlen=60
#step=01
#nx=-99
#ny=-99
#lat1=89.731
#lon1=-179.649
#polon=80.000
#polat=0.000
#dlon=0.400
#dlat=0.225
#
#Field=1 2 m temperature(K)
190 1 15.18 55.0 284.9 284.8 284.8 284.7 284.7 284.6 284.6 284.8 285.0 285.3 285.4 285.4 285.5 285.4 285.3 285.2 285.0 284.7 284.5 284.5 284.5 284.5 284.2 283.9 283.6 283.6 283.6 283.6 283.5 283.3 283.2 283.2 283.1 283.1 283.0 283.0 282.9 282.8 282.7 282.6 282.4 282.3 282.1 282.2 282.3 282.5 282.8 283.2 283.6 283.6 283.6 283.7 283.7 283.8 283.9 283.9 284.0 284.0 283.8 283.6 283.4
191 1 15.27 55.13 285.0 284.9 284.8 284.8 284.8 284.7 284.7 284.8 284.9 285.1 285.1 285.2 285.3 285.3 285.3 285.3 285.0 284.8 284.6 284.6 284.7 284.8 284.5 284.2 283.8 283.9 284.0 284.0 283.8 283.6 283.5 283.3 283.2 283.1 283.0 282.9 282.7 282.8 282.9 283.0 283.0 282.9 282.9 282.9 283.0 283.1 283.3 283.5 283.7 283.7 283.8 283.8 283.9 283.9 284.0 284.0 284.0 284.0 283.9 283.8 283.7
#Field=2 100 m temperature(K)
190 2 15.18 55.0 284.1 284.1 284.1 284.1 284.1 284.0 284.0 284.1 284.3 284.4 284.5 284.6 284.7 284.7 284.7 284.7 284.5 284.2 284.0 284.0 284.0 284.0 283.6 283.2 282.8 282.8 282.8 282.8 282.6 282.4 282.2 282.1 282.0 281.9 281.8 281.7 281.6 281.6 281.6 281.6 281.4 281.2 281.1 281.3 281.6 281.8 282.1 282.4 282.7 282.7 282.8 282.8 282.9 283.0 283.1 283.1 283.1 283.2 283.0 282.7 282.5
191 2 15.27 55.13 284.1 284.1 284.1 284.0 284.0 283.9 283.9 284.0 284.1 284.3 284.3 284.4 284.5 284.5 284.6 284.7 284.5 284.3 284.1 284.2 284.2 284.2 283.8 283.5 283.1 283.1 283.1 283.2 282.9 282.6 282.4 282.2 282.0 281.8 281.7 281.6 281.5 281.6 281.8 281.9 281.8 281.7 281.5 281.7 281.8 282.0 282.2 282.4 282.6 282.7 282.7 282.7 282.8 282.9 283.0 283.0 283.0 283.0 282.9 282.9 282.8
总共有5个现场。 我要做的是以以下格式读入数据帧:
"#Field-1" "2 m temperature(k)" "#Field-2" "100 m temperature(K)"
190 1 15.18 55.0 248.9.. 190 2 15.18 55.0 284.1...
191 1 15.27 55.13 285.0.. 191 2 15.27 55.13 284.1..
我试过以下方法:
colspecs = [(0, 8), (8, 1000)]
pd.read_fwf("ENERGINET_ECM_2017102600.dat",skiprows=13,colspecs=colspecs,sep=r"\s+",)
但这只返回2列,有没有办法指定在x行数之后需要一个新列?或者我应该使用不同的函数?你知道吗
编辑: 为结果集添加了值!你知道吗
尝试以下代码,该代码以列表的形式从文件中读取和获取数据,然后用于创建数据帧。添加解释作为注释:
输出:
注意:为了更清晰的显示,我将值截断为最初的5。代码应该适用于任意数量的行和字段。你知道吗
相关问题 更多 >
编程相关推荐