TypeError: int() argument must be a string, a bytes-like object or a number, not '_io.TextIOWrapper'
TypeError: int() argument must be a string, a bytes-like object or a number, not '_io.TextIOWrapper'
我正在尝试将文本文件逐行读取为整数。我做了我在这里看到的每一个建议,但 none 对我有用。这是我正在使用的代码。它从 datadir 中读取一些地震数据并评估 SNR 比率以决定是保留数据还是删除数据。为此,我需要计算站点与地震之间的距离,信息来自输入文件。
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 ()`
- 更新:
- 有两个输入文件:
- events.dat 相当于:
2020 03 18 17:45:39 -11.0521 115.1378
- all_st 相当于:
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 = str (time.split (":")[2]) # second
但是,紧接着,您对其进行了优化:
s = open (stationlist, "r")
现在 s
指向一个文件对象,因此 int(s)
失败并出现上述错误。将您的电台列表文件对象命名为不同的名称,问题就会消失。
其他可能对您有帮助的提示:
- split() 将自动按空格拆分,除非您另有说明,因此无需指定
" "
.
- 您可以使用多重赋值来分配
h
、m
和 s
,就像您对上一行所做的那样。目前,您在三个不同的时间执行相同的拆分操作。
- 建议使用
with
关键字打开文件,即使发生异常也会自动处理关闭文件。
- 您可以直接遍历文件对象,而无需使用
readlines()
创建列表。
- 使用 pathlib 可以使处理文件系统路径和分隔符变得更加简单和清晰。
- 在函数名和括号之间放置空格被认为是错误的形式。
- 还有一个约定,变量名(class 名称除外)通常都是小写字母,根据需要在单词之间加上下划线。 (请参阅 PEP 8 了解所有此类样式约定的有用概要。它们不是硬性规定,但它们可以帮助使代码更加一致和可读。)
考虑到这些因素,这里是您上述代码的稍微修饰过的版本:
from pathlib import Path
from obspy import UTCDateTime
data_dir = Path('/home/alireza/Desktop/Saman/Eqcomplete')
home_dir = Path('/home/alireza/Desktop/Saman')
event_list = home_dir / 'events.dat'
station_list = home_dir / 'all_st'
with open(event_list) as e_file:
for event in e_file:
year, mon, day, time, lat, lon = event.split()
h, m, s = time.split(':')
with open(station_list) as s_file:
for station in s_file:
stname, stlo, stla = station.split()
o_after_b = UTCDateTime(
int(year), int(mon), int(day), int(h), int(m), int(s)
)
print(o_after_b)
我正在尝试将文本文件逐行读取为整数。我做了我在这里看到的每一个建议,但 none 对我有用。这是我正在使用的代码。它从 datadir 中读取一些地震数据并评估 SNR 比率以决定是保留数据还是删除数据。为此,我需要计算站点与地震之间的距离,信息来自输入文件。
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 ()`
- 更新:
- 有两个输入文件:
- events.dat 相当于:
2020 03 18 17:45:39 -11.0521 115.1378
- all_st 相当于:
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 = str (time.split (":")[2]) # second
但是,紧接着,您对其进行了优化:
s = open (stationlist, "r")
现在 s
指向一个文件对象,因此 int(s)
失败并出现上述错误。将您的电台列表文件对象命名为不同的名称,问题就会消失。
其他可能对您有帮助的提示:
- split() 将自动按空格拆分,除非您另有说明,因此无需指定
" "
. - 您可以使用多重赋值来分配
h
、m
和s
,就像您对上一行所做的那样。目前,您在三个不同的时间执行相同的拆分操作。 - 建议使用
with
关键字打开文件,即使发生异常也会自动处理关闭文件。 - 您可以直接遍历文件对象,而无需使用
readlines()
创建列表。 - 使用 pathlib 可以使处理文件系统路径和分隔符变得更加简单和清晰。
- 在函数名和括号之间放置空格被认为是错误的形式。
- 还有一个约定,变量名(class 名称除外)通常都是小写字母,根据需要在单词之间加上下划线。 (请参阅 PEP 8 了解所有此类样式约定的有用概要。它们不是硬性规定,但它们可以帮助使代码更加一致和可读。)
考虑到这些因素,这里是您上述代码的稍微修饰过的版本:
from pathlib import Path
from obspy import UTCDateTime
data_dir = Path('/home/alireza/Desktop/Saman/Eqcomplete')
home_dir = Path('/home/alireza/Desktop/Saman')
event_list = home_dir / 'events.dat'
station_list = home_dir / 'all_st'
with open(event_list) as e_file:
for event in e_file:
year, mon, day, time, lat, lon = event.split()
h, m, s = time.split(':')
with open(station_list) as s_file:
for station in s_file:
stname, stlo, stla = station.split()
o_after_b = UTCDateTime(
int(year), int(mon), int(day), int(h), int(m), int(s)
)
print(o_after_b)