【问题标题】:How to move a tag and its contents within xml with bsoup?如何使用 bsoup 在 xml 中移动标签及其内容?
【发布时间】:2021-01-28 16:12:31
【问题描述】:

我有以下 xml:

</Page>
<Page ID="Page2" PHYSICAL_IMG_NR="2" HEIGHT="3300" WIDTH="2550">
<TopMargin HEIGHT="151" WIDTH="2550" VPOS="0" HPOS="0">
</TopMargin>
<LeftMargin HE<IGHT="2771" WIDTH="143" VPOS="151" HPOS="0">
</LeftMargin>
<RightMargin HEIGHT="2771" WIDTH="143" VPOS="151" HPOS="2407">
</RightMargin>
<BottomMargin HEIGHT="378" WIDTH="2550" VPOS="2922" HPOS="0">
<TextBlock>
</TextBlock>
</BottomMargin>
<PrintSpace>
</PrintSpace>
</Page>

我想将BottomMargin 标记及其内容移动到xml 的底部(在此示例中位于PrintSpace 标记之后)对于每个Page 标记

我该怎么做?

我正在通过以下方式读取 xml:

使用 open(xml, "r") 作为文件:

content = file.readlines()
content = "".join(content)

soup = bs.BeautifulSoup(content, 'lxml')

【问题讨论】:

    标签: python-3.x xml beautifulsoup xml-parsing


    【解决方案1】:

    您可以使用extract()insert_after()

    from bs4 import BeautifulSoup
    
    xml = """</Page>
    <Page ID="Page2" PHYSICAL_IMG_NR="2" HEIGHT="3300" WIDTH="2550">
    <TopMargin HEIGHT="151" WIDTH="2550" VPOS="0" HPOS="0">
    </TopMargin>
    <LeftMargin HE<IGHT="2771" WIDTH="143" VPOS="151" HPOS="0">
    </LeftMargin>
    <RightMargin HEIGHT="2771" WIDTH="143" VPOS="151" HPOS="2407">
    </RightMargin>
    <BottomMargin HEIGHT="378" WIDTH="2550" VPOS="2922" HPOS="0">
    <TextBlock>
    </TextBlock>
    </BottomMargin>
    <PrintSpace>
    </PrintSpace>
    </Page>"""
    
    
    soup = BeautifulSoup(xml, "html.parser")
    
    b_margin = soup.select_one("BottomMargin")
    
    # Remove BottomMargin from the xml
    for tag in soup.select("BottomMargin"):
        tag.extract()
    
    # add the `BottomMargin` to the `soup`
    soup.printspace.insert_after(b_margin)
    
    print(soup.prettify())
    

    输出:

    <page height="3300" id="Page2" physical_img_nr="2" width="2550">
     <topmargin height="151" hpos="0" vpos="0" width="2550">
     </topmargin>
     <leftmargin he<ight="2771" hpos="0" vpos="151" width="143">
     </leftmargin>
     <rightmargin height="2771" hpos="2407" vpos="151" width="143">
     </rightmargin>
     <printspace>
     </printspace>
     <bottommargin height="378" hpos="0" vpos="2922" width="2550">
      <textblock>
      </textblock>
     </bottommargin>
    </page>
    

    【讨论】:

      【解决方案2】:

      假设您的 XML 文档中有许多 &lt;page&gt;。不需要extract(),只需使用您的标签调用.insert_after()

      from bs4 import BeautifulSoup
      
      
      txt = '''
      <Page ID="Page1" PHYSICAL_IMG_NR="2" HEIGHT="3300" WIDTH="2550">
      <TopMargin HEIGHT="151" WIDTH="2550" VPOS="0" HPOS="0">
      </TopMargin>
      <LeftMargin HE<IGHT="2771" WIDTH="143" VPOS="151" HPOS="0">
      </LeftMargin>
      <RightMargin HEIGHT="2771" WIDTH="143" VPOS="151" HPOS="2407">
      </RightMargin>
      <BottomMargin HEIGHT="378" WIDTH="2550" VPOS="2922" HPOS="0">
      <TextBlock>
      </TextBlock>
      </BottomMargin>
      <PrintSpace>
      </PrintSpace>
      </Page>
             
      <Page ID="Page2" PHYSICAL_IMG_NR="2" HEIGHT="3300" WIDTH="2550">
      <TopMargin HEIGHT="151" WIDTH="2550" VPOS="0" HPOS="0">
      </TopMargin>
      <LeftMargin HE<IGHT="2771" WIDTH="143" VPOS="151" HPOS="0">
      </LeftMargin>
      <RightMargin HEIGHT="2771" WIDTH="143" VPOS="151" HPOS="2407">
      </RightMargin>
      <BottomMargin HEIGHT="378" WIDTH="2550" VPOS="2922" HPOS="0">
      <TextBlock>
      </TextBlock>
      </BottomMargin>
      <PrintSpace>
      </PrintSpace>
      </Page>'''
      
      
      soup = BeautifulSoup(txt, 'html.parser')
      
      for bottom_margin in soup.select('page bottommargin'):
          bottom_margin.find_parent('page').printspace.insert_after(bottom_margin)
      
      print(soup.prettify())
      

      打印:

      <page height="3300" id="Page1" physical_img_nr="2" width="2550">
       <topmargin height="151" hpos="0" vpos="0" width="2550">
       </topmargin>
       <leftmargin he<ight="2771" hpos="0" vpos="151" width="143">
       </leftmargin>
       <rightmargin height="2771" hpos="2407" vpos="151" width="143">
       </rightmargin>
       <printspace>
       </printspace>
       <bottommargin height="378" hpos="0" vpos="2922" width="2550">
        <textblock>
        </textblock>
       </bottommargin>
      </page>
      <page height="3300" id="Page2" physical_img_nr="2" width="2550">
       <topmargin height="151" hpos="0" vpos="0" width="2550">
       </topmargin>
       <leftmargin he<ight="2771" hpos="0" vpos="151" width="143">
       </leftmargin>
       <rightmargin height="2771" hpos="2407" vpos="151" width="143">
       </rightmargin>
       <printspace>
       </printspace>
       <bottommargin height="378" hpos="0" vpos="2922" width="2550">
        <textblock>
        </textblock>
       </bottommargin>
      </page>
      

      【讨论】:

        猜你喜欢
        • 2013-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-11
        • 2021-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多