【问题标题】:TypeError: int() argument must be a string, a bytes-like object or a number, not '_io.TextIOWrapper' [closed]TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'_io.TextIOWrapper' [关闭]
【发布时间】:2021-12-04 08:35:45
【问题描述】:

我正在尝试将文本文件逐行读取为整数。我做了我在这里看到的每一个建议,但没有一个对我有用。这是我正在使用的代码。它从 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 ()`
  • 更新:
  • 有两个输入文件:
  1. events.dat 类似于:
2020 03 18 17:45:39 -11.0521 115.1378
  1. 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 可能有用。

【问题讨论】:

  • 你调试了吗? h、m、s 变量的值是多少?
  • 我尝试了我所知道的一切。我不是python专家。 h、m 和 s 是小时、分钟和秒。您可以看到示例输入。
  • 我看不出这个错误是如何由您显示的代码引起的。听起来您可能向int() 提供了一个文件对象。你能提供完整的堆栈跟踪,而不仅仅是简单的错误消息吗? (此外,您的代码不能单独测试。请仔细阅读minimal, reproducible example 的预期内容。)
  • 包含其余代码确实使您的示例完整且可运行,因此做得很好。但这远非最小……这是很多与您的问题无关的代码,供其他人尝试帮助您。我建议尝试MRE page 上“最小”下描述的两种方法中的任何一种。奖励:它还可以帮助您更好地调试。
  • 更新:我无法通过发布的代码和示例数据重现此错误。它可能以某种方式涉及数据文件的其他部分。您是否使用此特定子集测试了您的代码以查看它是否仍会产生错误?

标签: python typeerror


【解决方案1】:

你在这里定义s

s = str (time.split (":")[2])   # second

但是,紧接着,你会改进它:

s = open (stationlist, "r")

现在s 指向一个文件对象,所以int(s) 失败并出现上述错误。将您的电台列表文件对象命名为不同的名称,问题就会消失。


其他可能对您有帮助的提示:

  • split() 将自动在空白处拆分,除非您另有说明,因此无需指定 " "
  • 您可以使用多重赋值来分配hms,方法与上一行相同。目前,您正在三个不同的时间执行相同的拆分操作。
  • 建议使用with关键字打开文件,即使出现异常也会自动处理关闭文件。
  • 您可以直接遍历文件对象,而无需使用readlines() 创建列表。
  • 使用pathlib 可以更简单、更清晰地处理文件系统路径和分隔符。
  • 在函数名称和括号之间放置空格被认为是一种错误的形式。
  • 还有一个约定,变量名(除了类名)通常都是小写的,根据需要在单词之间加上下划线。 (请参阅 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)

【讨论】:

  • 感谢您节省我的时间和指导。
猜你喜欢
  • 2020-08-22
  • 2016-12-06
  • 1970-01-01
  • 2017-11-14
  • 2019-09-16
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多