指定x行数后的新列,使用read从.dat文件读取_

2024-09-28 01:27:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将.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行数之后需要一个新列?或者我应该使用不同的函数?你知道吗

编辑: 为结果集添加了值!你知道吗


Tags: 文件数据视图fielddate格式stepdat
1条回答
网友
1楼 · 发布于 2024-09-28 01:27:48

尝试以下代码,该代码以列表的形式从文件中读取和获取数据,然后用于创建数据帧。添加解释作为注释:

# READ ALL LINES
with open("tempfile.dat", "r") as f:
    lines = f.readlines()

# GET COLUMN NAMES: 
colnames = []
for line in lines:
    if line.startswith("#Field="):
        words = line.split()
        colnames.append(words[0])
        colnames.append(" ".join(words[1:]))

# REMOVE LINES STARTING WITH #:
newlines = []
for line in lines:
    if not line.startswith("#"):
        newlines.append(line)

# GET ALL FIELD NAMES, WITHOUT DUPLICATING:  
fldnames = []
for line in newlines:
    name = line.split()[0]
    if name not in fldnames:
        fldnames.append(name)

# READ ALL ROWS TO CREATE A LIST OF LISTS FOR DATAFRAME: 
allrows = []
for name in fldnames: 
    onerow = []
    for line in newlines:
        words = line.split()
        if words[0] == name:
            onerow.append(words[0])
            onerow.append(words[1:])
    allrows.append(onerow)

# CREATE DATAFRAME: 
df = pd.DataFrame(data=allrows, columns=colnames)
print(df)

输出:

  #Field-1               2 m temperature(k) #Field-2               #100 m temperature(K)  
0      190   [1, 15.18, 55.0, 284.9, 284.8]      190      [2, 15.18, 55.0, 284.1, 284.1]  
1      191  [1, 15.27, 55.13, 285.0, 284.9]      191     [2, 15.27, 55.13, 284.1, 284.1]  

注意:为了更清晰的显示,我将值截断为最初的5。代码应该适用于任意数量的行和字段。你知道吗

相关问题 更多 >

    热门问题