【问题标题】:XML file processing subtags or make the number of subtags consistentXML文件处理子标签或使子标签数量一致
【发布时间】:2019-10-10 01:32:00
【问题描述】:

处理后的XML文件内容如下:

<dblp>
<incollection> 
<author>Philippe Balbiani</author> 
<author>Valentin Goranko</author> 
<author>Ruaan Kellerman</author> 
<booktitle>Handbook of Spatial Logics</booktitle> 
</incollection>
<incollection> 
<author>Jochen Renz</author> 
<author>Bernhard Nebel</author> 
<booktitle>Handbook of AI</booktitle> 
</incollection>
...
</dblp>

格式内容如上图提取“author”标签内容和“booktitle”标签内容它们都在“incollection”标签中遍历每个“incollection”标签并有多个“author”标签内容带有“书名”标签的内容形式对应的元组。

我的代码:

soup = BeautifulSoup(str(getfile()), 'lxml')
res = soup.find_all('incollection')
author = []
booktitle =[]

for each in res:
    for child in each.children:
          if child.name == 'author':
                author.append(child.text)
          elif child.name == 'booktitle': 
                booktitle.append(child.text)
elem_dic = tuple(zip(author, booktitle))

我做的结果是:

('Philippe Balbiani', 'Handbook of Spatial Logics')
('Valentin Goranko', 'Handbook of Spatial Logics')
('Ruaan Kellerman', 'Handbook of Spatial Logics')

如何修改它以达到预期的效果?像这样:

('Philippe Balbiani', 'Handbook of Spatial Logics')
('Valentin Goranko', 'Handbook of Spatial Logics')
('Ruaan Kellerman', 'Handbook of Spatial Logics')
('Jochen Renz', 'Handbook of AI')
('Bernhard Nebel', 'Handbook of AI')

或者你可以在每个“incollection”标签中添加“booktitle”标签到与“author”标签相同的数字。

【问题讨论】:

    标签: python html xml python-3.x python-2.7


    【解决方案1】:

    假设 BeautifulSoup 4.7+

    这实际上很容易做到。在这个例子中,我使用了选择器(我知道选择器通常与 HTML 相关联,但您可以在 XML 中使用它们来完成此类任务。这里我们说我们想要所有具有直接子级的 incollection 标记( &gt;) 的标签是 author 或 booktitle (:is(author, booktitle))。这只会给我们提供我们感兴趣的标签。然后我们简单地收集作者,直到我们看到书名,然后创建条目为那本书。之后我们重置并收集下一本书的信息:

    from bs4 import BeautifulSoup
    
    markup = """
    <dblp>
    <incollection> 
    <author>Philippe Balbiani</author> 
    <author>Valentin Goranko</author> 
    <author>Ruaan Kellerman</author> 
    <booktitle>Handbook of Spatial Logics</booktitle> 
    </incollection>
    <incollection> 
    <author>Jochen Renz</author> 
    <author>Bernhard Nebel</author> 
    <booktitle>Handbook of AI</booktitle> 
    </incollection>
    </dblp>
    """
    
    author = []
    elem_dic = []
    
    soup = BeautifulSoup(markup, 'xml')
    for child in soup.select('incollection > :is(author,booktitle)'):
        if child.name == 'author':
            author.append(child.text)
        else:
            elem_dic.extend(zip(author, [child.text] * len(author)))
            author = []
    
    print(tuple(elem_dic))
    

    输出

    (('Philippe Balbiani', 'Handbook of Spatial Logics'), ('Valentin Goranko', 'Handbook of Spatial Logics'), ('Ruaan Kellerman', 'Handbook of Spatial Logics'), ('Jochen Renz', 'Handbook of AI'), ('Bernhard Nebel', 'Handbook of AI'))
    

    您不必使用选择器:

    from bs4 import BeautifulSoup, Tag
    
    markup = """
    <dblp>
    <incollection> 
    <author>Philippe Balbiani</author> 
    <author>Valentin Goranko</author> 
    <author>Ruaan Kellerman</author> 
    <booktitle>Handbook of Spatial Logics</booktitle> 
    </incollection>
    <incollection> 
    <author>Jochen Renz</author> 
    <author>Bernhard Nebel</author> 
    <booktitle>Handbook of AI</booktitle> 
    </incollection>
    </dblp>
    """
    
    author = []
    elem_dic = []
    
    soup = BeautifulSoup(markup, 'xml')
    res = soup.find_all('incollection')
    for each in res:
        for child in each.children:
            if not isinstance(child, Tag):
                continue
            if child.name == 'author':
                author.append(child.text)
            else:
                elem_dic.extend(zip(author, [child.text] * len(author)))
                author = []
    
    print(tuple(elem_dic))
    

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      相关资源
      最近更新 更多