python - 如何以前面规定的不同方式读取大块的行,触发 Python 中的文本

标签 python macos loops

虽然我完全是 Python 的初学者,但使用它进行编程非常有趣。我在 OSX 上使用 Python 解释器 2.7.5。我正在尝试做的是从我首先使用 BeautifulSoup 处理的 HTML 文件中读取大量行以获取文本内容。因此,我想通过前面的触发文本来区别对待这些所需的行 block 。但是,我的代码无法区分这两个触发器,并通过“触发器类型 1”处理所有内容。如果我能找到这个问题的解决方案,我会添加更多不同的触发器类型。

首先读取 HTML 剥离的 temp_file.txt:

lots of unnecessary data on these lines
TRIGGER Type 1
lots of unnecessary data on these lines
Name:
String_to_be_found1
unnecessary data line
54301957(the desired number)

lots of unnecessary data on these lines
TRIGGER Type 2
lots of unnecessary data on these lines
Name2:
String_to_be_found2
unnecessary data line
unnecessary data line
unnecessary data line
54139851(the desired number)

lots of unnecessary data on these lines

TRIGGER Type 1
lots of unnecessary data on these lines
Name:
String_to_be_found3
unnecessary data line
425827459(the desired number)

还有我的代码:

f = open("temp_file.txt", "r+")
f2 = open("my_output_file.txt", 'w')

for line in iter(f.readline, ''):
    if 'TRIGGER' in line:
        if 'Type 1' in line:
            for line in f:
                if 'Name' in line:
                    desired_string = f.next()
                    f.next()
                    desired_number=f.next()
                    f2.write(desired_string + desired_number + '\n')
        if 'Type 2' in line:
                if 'Name2' in line:
                    f.next()
                    desired_string = f.next()
                    f.next()
                    f.next()
                    f.next()
                    desired_number=f.next()
                    f2.write(desired_string + desired_number + '\n')
f.close()
f2.close()

我一直在为这个问题苦苦思索。非常感谢任何帮助。

最佳答案

你的代码有点错误,如果 Type 1 是第一个找到的类型,那么它将把之后的所有行都视为 Type 1 就像你一样在其中循环遍历 f 并且在到达文件末尾之前您不会退出该内部循环。它不会那样工作。

您应该考虑定义一种标志,根据触发器的类型,当您在其中找到包含 TRIGGER 的行时,您会更改该标志。

此外,您可以直接遍历 file ,它会遍历每一行,您不需要遍历 iter(f.readline, '') .

例子-

f = open("temp_file.txt", "r+")
f2 = open("my_output_file.txt", 'w')

flag = None
for line in f:
    if 'TRIGGER' in line:
        if 'Type 1' in line:
            flag = 1
        elif 'Type 2' in line:
            flag = 2
    elif flag == 1:
        <do processing for `Type 1`>
    elif flag == 2:
        <do processing for `Type 2`>
f.close()
f2.close()

关于python - 如何以前面规定的不同方式读取大块的行,触发 Python 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31993963/

相关文章:

python - 运行单独的 python 进程是否避免了 GIL?

python - 如何乘以列表中的某个整数?

macos - 按路径将应用程序置于最前面

swift - 从 native MacOS 应用程序访问 Web 浏览器选项卡并读取网站信息

c - 使用临时变量或直接从数组更有效?

python - 如何使用python追加多维数组?

python postgres 在读取结果时更改行的值

python - Django Rest Framework 在 POST 中接收主键值并将模型对象作为嵌套序列化程序返回

c# - 用于非窗口操作系统的单声道混淆器

linux - 如何一次获得您对配置文件所做的所有更改(自系统安装以来)?