Python txt 文件标签解析

1 投票
2 回答
1524 浏览
提问于 2025-04-18 16:45

我正在尝试从一个文本文件中提取两个不同标签的内容。我能成功获取到第一个标签“p”的所有实例,但第二个标签“l”的内容却没有找到。请问问题出在“或”这个地方吗?

谢谢你的帮助。这是我正在使用的代码:

with open('standardA00456.txt','w') as output_file:
    with open('standardA00456.txt','r') as open_file:
            the_whole_file = open_file.read()
            start_position = 0

            while True:

               start_position = the_whole_file.find('<p>' or '<l>', start_position)

               end_position = the_whole_file.find('</p>' or '</l>', start_position)
               data = the_whole_file[start_position:end_position+5]


               output_file.write(data + "\n")
               start_position = end_position

2 个回答

0

你是英语专业的,我觉得你需要改善一下逻辑。我修改了你的代码,得到了这个:

with open('standardA00456.txt','w') as output_file:
    with open('standardA00456.txt','r') as open_file:
        the_whole_file = open_file.read()
        start_position = 0

        found_p = False
        fould_l = False

        while True:
            start_pos_p = the_whole_file.find('<p>', start_position)
            start_pos_l = the_whole_file.find('<l>', start_position)

            if start_pos_p > -1 and start_pos_l > -1:
                if start_pos_p < start_pos_l:
                    found_p = True
                    start_position = start_pos_p
                    found_l = False
                else:
                    found_l = True
                    start_position = start_pos_l
                    found_p = False
            elif start_pos_p > -1:        
                found_p = True
                start_position = start_pos_p
                found_l = False
            elif start_pos_l > -1:        
                found_l = True
                start_position = start_pos_l
                found_p = False
            else:
                break

            if found_p:
                end_position = the_whole_file.find('</p>', start_position)

            elif found_l:
                end_position = the_whole_file.find('</l>', start_position)

            else:
                break

            data = the_whole_file[start_position:end_position+5]
            output_file.write(data + "\n")
            start_position = end_position
1

在这里,'<p>' 或 '<l>' 总是等于 '<p>',因为这段代码告诉 Python 只有在 '<p>'NoneFalse、数字零或者空的时候,才使用 '<l>'。而由于字符串 '<p>' 永远不会是这些情况,所以 '<l>' 总是会被跳过:

>>> '<p>' or '<l>'
'<p>'
>>> None or '<l>'
'<l>'

你可以很简单地使用 re.findall

import re
with open('standardA00456.txt','w') as out_f,  open('standardA00456.txt','r') as open_f:
    p_or_ls = re.findall(r'(?:<p>.*?</p>)|(?:<l>.*?</l>)', 
                         open_f.read(), 
                         flags=re.DOTALL) #to include newline characters
    for p_or_l in p_or_ls:
        out_f.write(p_or_l + "\n")

不过,使用正则表达式来解析带标签的文件(比如 HTML 和 XML)其实并不是个好主意。使用像 BeautifulSoup 这样的模块会更安全:

from bs4 import BeautifulSoup
with open('standardA00456.txt','w') as out_f,  open('standardA00456.txt','r') as open_f:
    soup = BeautifulSoup(open_f.read())
    for p_or_l in soup.find_all(["p", "l"]):
        out_f.write(p_or_l + "\n")

撰写回答