【发布时间】:2020-09-14 17:03:36
【问题描述】:
我从来没有处理过从 ASCII 文件导入数据的工作,而且我注意到不同的 ASCII 文件有不同的格式,因此尝试找到适用于任何格式的通用解决方案对我来说具有挑战性。
我有一个.dat (ASCII) 文件,我需要读取它并提取变量(请参阅问题底部的 txt 的 sn-p)。以下是我试图弄清楚如何读取数据的不同尝试(以###分隔)的代码。
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.
【问题讨论】:
-
文本图像不是很有用,因为我们无法解析它们!不鼓励使用代码和数据的图片。
-
我已经编辑了帖子以包含文件链接。
-
我不想点击链接 - 请编辑相关示例,将您的 dat 文件文本编辑到您的问题中。
-
我已更新问题以包含 txt 文件的相关示例并删除了链接。感谢您分享如何更好地提问的技巧。
标签: python csv import ascii genfromtxt