我正在尝试读取固定宽度格式的文件,并通过在函数中使用正则表达式来标识页眉和页脚行,从而跳过数量可变的页眉和页脚行。我正在使用python3.7和pandas 0.25.1:
import re
import pandas as pd
class Skewt():
def create_skewt(self, datafile):
# Read in the column names from the header. For a description, see:
# http://weather.uwyo.edu/upperair/columns.html
col_names = pd.read_fwf(datafile, header=4, nrows=1).columns
col_names = col_names[0].split()
print(col_names)
# Read the data from the data file into a pandas dataframe
# Only read in columns we need [P, T, Td], and skip header and footer
# lines (identified by having letters or 2 or more dashes)
header = re.compile(r'[A-Za-z-][A-Za-z-]')
rdat = pd.read_fwf(datafile, skiprows= lambda x: x in header.match(x),
usecols=[0, 2, 3], names=col_names)
if __name__ == "__main__":
datafile = "../../test/data/726722019052812.ctrl.mtp"
skewt = Skewt()
skewt.create_skewt(datafile)
plt.show()
这是数据文件的一个示例
"<HTML>
<H2>72469 DNR Denver Observations at 12Z 16 Sep 2019</H2>
<PRE>
-----------------------------------------------------------------------------
PRES HGHT TEMP DWPT RELH MIXR DRCT SKNT THTA THTE THTV
hPa m C C % g/kg deg knot K K K
-----------------------------------------------------------------------------
1000.0 59
925.0 745
850.0 1475
837.0 1611 16.4 6.4 52 7.25 0 0 304.6 326.8 306.0
当我运行时,出现以下错误:
TypeError: argument of type 'function' is not iterable
两年前的这个问题Pandas read_fwf error when skiprows is a function建议更新熊猫,但我相信我有最新的版本。你知道吗
是否取消了对回调的支持?我还缺什么吗?(我对熊猫还不熟悉。)
目前没有回答
相关问题 更多 >
编程相关推荐