【问题标题】:part text inside tags python标签内的部分文本python
【发布时间】:2018-03-31 08:15:52
【问题描述】:

我有一个半结构化的 .txt 文件。该文件如下所示:

<tags>
    blabla<text>
              I want this
         </text>
    blabla<text>
               And this
           </text>
        bla<text>
                 and this
            </text>blabla
</tags>

我想获取 &lt;text&gt; 标签内的文本。我已经设法使用字符串分区和替换来做到这一点,但我认为它不是非常有效或漂亮。

这是我的代码:

with open('collection.txt') as f:
 read_data = f.read()

text1 = read_data.partition("<text>")[2].partition("</text>")[0]
temp1 = read_data.replace(text1,'').replace('<text>','',1).replace('</text>','',1)
text2 = temp1.partition("<text>")[2].partition("</text>")[0]
temp2 = read_data.replace(text2,'').replace('<text>','',2).replace('</text>','',2)
text3 = temp2.partition("<text>")[2].partition("</text>")[0]

BeautifulSoup、元素树和其他 XML 解析器不起作用。 关于如何改进我的代码的任何建议?我试过编译一个正则表达式,但无济于事。

【问题讨论】:

  • 使用正则表达式!如果您以前没有使用过它,这有点学习曲线,但对于这样的项目来说绝对值得
  • @Aran-Fey 我已经尝试过包括 BeautifulSoup 在内的那些解析器,但我无法让它工作,可能是因为文件是半结构化的?
  • @EriktheRed 我认为正则表达式是我想要解决的方法,但到目前为止我尝试过的每个正则表达式都打印空白。
  • 不建议将正则表达式用于结构化/嵌套解析。对于更复杂的示例,它很快就会失败。

标签: python beautifulsoup text-extraction


【解决方案1】:

使用 XML 解析器,例如 xml.etree (live demo):

import xml.etree.ElementTree as ET
doc = ET.parse('collection.txt')
print([el.text.strip() for el in doc.findall('.//text')])
# output: ['I want this', 'And this', 'and this']

【讨论】:

    【解决方案2】:

    您可以按如下方式使用 BeautifulSoup 获取所有文本条目:

    from bs4 import BeautifulSoup
    
    with open('collection.txt') as f:
        read_data = f.read()
    
    soup = BeautifulSoup(read_data, 'xml')
    
    for text in soup.find_all('text'):
        print(text.get_text(strip=True))
    

    给你:

    I want this
    And this
    and this
    

    您绝对应该避免尝试使用正则表达式来进行这种解析,因为对于更复杂的示例,它会很快失败,例如如果在数据中间使用了注释,例如&lt;!-- &lt;/text&gt; --&gt;,则应该忽略它。

    【讨论】:

    • 谢谢,这很有魅力,我显然错误地实现了 BeautifulSoup。
    【解决方案3】:

    正则表达式是你最好的朋友!


    import re
    
    p = re.compile(r'<text>([^</]*)</text>')
    result = p.findall(data_txt)
    result = [x.strip() for x in result]
    print(result)
    

    【讨论】:

    【解决方案4】:
    re.findall('<text>\s*.*\s*</text>', data)
    

    另一种解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-08
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多