我试图以整数的形式逐行读取文本文件。我做了我在这里看到的每一个建议,但没有一个对我有效。这是我正在使用的代码。它从datadir读取一些地震数据,并评估信噪比,以决定是保留数据还是删除数据。要做到这一点,我需要计算台站和地震之间的距离,这些信息来自输入文件
from obspy import UTCDateTime
import os
datadir = "/home/alireza/Desktop/Saman/Eqcomplete"
homedir = "/home/alireza/Desktop/Saman"
eventlist = os.path.join (homedir, 'events.dat')
stationlist = os.path.join (homedir, 'all_st')
e = open (eventlist, "r")
for event in e.readlines():
year, mon, day, time, lat, lon = event.split (" ")
h = str (time.split (":")[0]) # hour
m = str (time.split (":")[1]) # minute
s = str (time.split (":")[2]) # second
s = open (stationlist, "r")
for station in s.readlines():
stname, stlo, stla = station.split (" ")
OafterB = UTCDateTime (int(year), int(mon), int(day), int(h), int(m), int(s))
print (OafterB) # just to have an output!
s.close ()
e.close ()`
2020 03 18 17:45:39 -11.0521 115.1378
AHWZ 48.644 31.430
AFRZ 59.015 33.525
NHDN 60.050 31.493
BDRS 48.881 34.054
BMDN 48.825 33.772
HAGD 49.139 34.922
以下是输出:
Traceback (most recent call last):
File "SNR.py", line 21, in <module>
OafterB = UTCDateTime (int(year), int(mon), int(day), int(h), int(m), int(s))
TypeError: int() argument must be a string, a bytes-like object or a number, not '_io.TextIOWrapper'
这里测试安装obspy包所需的代码。
pip install obspy
可能有效
您可以在此处定义
s
:但是,紧接着,你就对它进行了改进:
现在
s
指向一个文件对象,因此int(s)
由于上述错误而失败。将站点列表文件对象命名为不同的名称,问题就会消失您可能会发现其他有用的提示:
" "
李>h
、m
和s
,方法与前一行相同。目前,您正在三次执行相同的拆分操作李>readlines()
创建列表李>考虑到这些因素,下面是上述代码的一个稍加修饰的版本:
相关问题 更多 >
编程相关推荐