以ASCII格式读取不常见的ASCII文件

问题描述 投票:0回答:1

我从没用过从ASCII文件导入数据的工作,并且我注意到不同的ASCII文件具有不同的格式,因此试图找到适用于任何格式的通用解决方案对我来说是充满挑战的。

我有一个.dat(ASCII)文件,需要读入并提取变量(请参阅问题底部的txt片段)>)。下面是我尝试不同方式(用###隔开)的代码,试图弄清楚如何读取数据。

f_41 = open(fileRS41, 'r')
data_41 = f_41.read()
for line in data_41:
    print(repr(line))
data_41.close()

############################

f = open(fileRS41, 'r')

# Read and ignore header lines
header1 = f.readline()
header2 = f.readline()
header3 = f.readline()

# Loop over lines and extract variables of interest
for line in f:
    line = line.strip()
    columns = line.split()
    name = columns[1] # Not sure what the different numbers do but this was code from another solution
    j = float(columns[1]) # ERROR: string can't be converted to float
    print(name, j)
f.close()

############################
from astropy.io import ascii
data = ascii.read(f_41, guess=False)  
print(data) 
############################
x = np.genfromtxt(f_41, dtype=None)

另一种选择是先将其转换为CSV文件,然后使用Pandas对其进行处理。但是,当我进行转换时,变量名被导入为一列,彼此堆叠在一起,而每列对应一个变量名。

# convert ASCII to CSV
f = open(file, 'r')
lines = f.readlines()

with open("FILEOUT.csv", 'w') as csvfile:
    writer = csv.writer(csvfile)
    for l in lines:
        asdf = l.split()
        writer.writerow(asdf)
print("out?")

。dat文件相关示例:

Generated by Rfunction:  Get.mw41.edt.func2 
============> Radisonde_info:
RS_type:        RS41-SGP
RS_config:      -32768
RS_serialnum:   R3340183
RS_freq:        403
RS__windtype:   ccGPS
=============> Station_info:
Station:        HUBV_RS41SGP
Latitude:       39.0563
Longitude:      -76.8755
Altitude:       52.3
SW version:     MW41 2.15.0
Start time:     2020-01-23 06:46:41
=============> Variables & units - Vaisala EDT
NA_numeric value:  -9999
NA_string:  xx or NA
-----------------------------
      Variable       Unit
          time        sec
            xx         NA
            Ta          K
            RH          %
       v(S->N)        m/s
       u(E->W)        m/s
        Height          m
         press        hPa
            Td          K
            MR       g/Kg
            DD        dgr
            FF        m/s
    Ascend_FLG  (0-N,1-Y)
            xx         NA
            xx         NA
           Lon        dgr
           Lat        dgr
            xx         NA
            xx         NA
            xx         NA
=============> Data:
   0.00  -9999.    268.37    85.00      0.00      0.00         52.3   1023.19    266.24     2.22       0.00      0.00 1  -9999.  -9999.   -76.8755    39.0563  -9999.  -9999.  -9999.
   0.81  -9999.    268.46    83.38      0.46      0.86         54.5   1022.90    266.08     2.19     241.86      0.98 1  -9999.  -9999.   -76.8757    39.0564  -9999.  -9999.  -9999.

我从没用过从ASCII文件导入数据的工作,我已经注意到不同的ASCII文件具有不同的格式,因此试图找到一种适用于任何格式的通用解决方案已被证明...

python csv ascii genfromtxt importdata
1个回答
0
投票
我找到了一个可行的解决方案,但我想避免对列标题进行硬编码,并能够直接从ASCII文件读取它们:
© www.soinside.com 2019 - 2024. All rights reserved.