我有多個 .dat 檔案,所有這些檔案都以關于觀察時間、緯度、經度和...的類似資訊開頭。在此文本資訊之后,有 16 列觀測資料。看起來像這樣:
#Occultation start (UTC): 2022-01-01 00:10:00
#Occultation stop (UTC): 2022-01-01 00:12:04
#Occultation point latitude (degN): -59.5
#Occultation point longitude (degE): -54.6
#Center of curvature (m): 3264.126 -4599.850 27305.984
#Radius of curvature (m): 6382932.736
#Azimuth (degN): 177.809
#Undulation (m): 20.772
#Elevation (m): 0.000
#Processing method: Wave optics (CT2)
#Background data: ECMWF forecast
#Receiver data type: L1caL2c
#Navbit correction: extern
#Occultation type: Rising
#OL/CL mode: OL->CL
#Alt OL/CL (m): 8821
#alt w.r.t. EGM96
geoid|lat|lon|azimuth|ba_opt|impact_opt|refrac|dry_press|dry_temp|geopotential height|ba_raw|ba_L1|ba_L2|ba_model|snr_L1|snr_L2
0.000 -99999000.000 -99999000.000 -99999000.000 -0.99999E 08 -99999000.000 -0.99999E 08 -99999000.000 -99999000.000 -99999000.000 -0.99999E 08 -0.99999E 08 -0.99999E 08 -0.99999E 08 -99999000.000 -99999000.000
我想做的是:
遍歷多個 .dat 檔案并提取緯度和經度資料并繪制它們。我可以用一個檔案做到這一點,但在回圈中我得到了錯誤。這段代碼可以給我經緯度,但由于某些未知原因,運行它后我收到一個錯誤:
f = open('/Users/file.dat','r')
data = f.read()
a = data.split
lat=a[14:15]
lon=a[19:20]
lat
TypeError Traceback (most recent call last)
<ipython-input-1-4f169d6b0f6f> in <module>
2 data = f.read()
3 a = data.split
----> 4 lat=a[14:15]
5
6 lon=a[19:20]
TypeError: 'builtin_function_or_method' object is not subscriptable
這是我的回圈:
x=[]
y=[]
for file in pathlist:
with open (file, 'r') as input:
dataset= file.read()
a=dataset.split
lat=a[14:15]
lon=a[19:20]
dlat=x.append[lat]
dlon=y.append[lon]
我收到這個錯誤:
AttributeError: 'str' object has no attribute 'read'
uj5u.com熱心網友回復:
您忘記了呼叫該方法的括號。您可以通過將值傳遞給sep-argument(默認為空格)來指定拆分標準,有關詳細資訊,請參閱doc。
a = data.split()
那么切片a[14:15]將有意義,因此TypeError: 'builtin_function_or_method' object is not subscriptable應該修復。
要解決此問題,AttributeError: 'str' object has no attribute 'read'您需要將該read方法應用于檔案描述符而不是檔案名。[做影子內置函式的名字,input是內置的!]
for file in pathlist:
with open (file, 'r') as fd:
dataset = fd.read()
uj5u.com熱心網友回復:
使用re,
import re
for file in pathlist:
with open (file, 'r') as fp:
lines = fp.readlines()
latitude = re.match(r'.* ([-\.\d] )', lines[2]).group(1)
longitude = re.match(r'.* ([-\.\d] )', lines[3]).group(1)
print(latitude, longitude)
結果:
-59.5 -54.6
您可以使用re來查找緯度和經度。拆分整個檔案資料并根據位置查找值,這可能很有可能得到錯誤的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/519841.html
