gpt4 book ai didi

Python - 使用 readlines() 处理第 n 行跃点

转载 作者:行者123 更新时间:2023-11-30 23:31:02 25 4
gpt4 key购买 nike

我正在尝试修复一个我想在 Github 上使用的损坏的库。

我已在本地“修复”了该问题。但我不认为这是一个非常干净的方法......

我正在通过互联网存档查看 WARC 库,特别是 arc.py 部分 ( https://github.com/internetarchive/warc/blob/master/warc/arc.py )。

自从编写 lib 以来,制作 ARC 文件的工具已经发生了一些变化,因此,内置解析器失败,因为它不希望看到文件中的某些元数据。

我的本​​地修复如下所示:

    if header.startswith("<arcmetadata"):
while not header.endswith("</arcmetadata>\n"):
header = self.fileobj.readline()
header = self.fileobj.readline()
header = self.fileobj.readline()

而且我不确定我两次调用 readlines() 来删除接下来的两个空行(包含 "/n" 是最干净的方法推进文件对象。

这是好Python吗?或者有更好的方法吗?

最佳答案

该代码看起来像是复制/粘贴错误。使用 .readline() 没有任何问题,只需记录您正在做的事情即可:

# skip metadata
if header.startswith("<arcmetadata"):
while not header.endswith("</arcmetadata>\n"):
header = self.fileobj.readline()
#NOTE: header ends with `"</arc..."` here i.e., it is not blank

# skip blank lines
while not header.strip():
header = self.fileobj.readline()

顺便说一句,如果文件包含 xml,则使用 xml 解析器来解析它。不要用手做。

关于Python - 使用 readlines() 处理第 n 行跃点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20204757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com