【问题标题】:Python. Get Internal XML with no whitespacePython。获取没有空格的内部 XML
【发布时间】:2020-12-08 18:00:59
【问题描述】:

我有一个这样的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <head>
        <version>1.0</version>
        <project>hello, world</project>
        <date>2020-08-15</date>
    </head>
    <file name="helloworld.py"/>
    <file name="helloworld.ps1"/>
    <file name="helloworld.bat"/>
</data>

我需要抓取 head 元素中的数据,元素之间没有空格,像这样:

<version>1.0</version><project>hello, world</project><date>2020-08-15</date>

然后散列它。现在,我必须做一些字符串操作才能把它变成一行:

root = ET.parse('myfile.xml').getroot()
header = ET.tostring(root[0]).decode('utf-8')
import re
header = re.sub('\n','',header)
header = re.sub('>\s+<','><',header)
header = header.replace('<head>','')
header = header.replace('</head>','')
header = header.strip()

有没有更简单的方法来做到这一点? Powershell XML 对象有一个简单的 InnerXML 属性,该属性为您提供元素内的 XML,没有空格作为字符串。 Python 有没有一种方法可以使这更容易?

【问题讨论】:

    标签: python xml removing-whitespace


    【解决方案1】:

    如果您可以使用外部库,BeautifulSoup 在这方面做得很好。

    https://www.crummy.com/software/BeautifulSoup/bs4/doc/#making-the-soup

    这是您的文档的示例。

    from bs4 import BeautifulSoup as bs
    
    xml_doc = """<?xml version="1.0" encoding="UTF-8"?>
     <data>
     <head>
         <version>1.0</version>
         <project>hello, world</project>
         <date>2020-08-15</date>
     </head>
     <file name="helloworld.py"/>
     <file name="helloworld.ps1"/>
     <file name="helloworld.bat"/>
    </data>"""
    
    page_soup = bs(xml_doc)
    
    page_soup.head.getText()
    
    page_soup.head.getText().strip().replace('\n','').replace(' ','')
    

    这将返回 head 标签的子元素的内容,并去掉换行符和空格。

    【讨论】:

      【解决方案2】:

      下面(不使用任何外部库 - 只是核心 python)

      import xml.etree.ElementTree as ET
      
      root = ET.parse('input.xml')
      head = root.find('.//head')
      combined = ''.join(['<{}>{}</{}>'.format(e.tag,e.text,e.tag) for e in list(head)])
      print(combined)
      

      输入.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <data>
          <head>
              <version>1.0</version>
              <project>hello, world</project>
              <date>2020-08-15</date>
          </head>
          <file name="helloworld.py"/>
          <file name="helloworld.ps1"/>
          <file name="helloworld.bat"/>
      </data>
      

      输出

      <version>1.0</version><project>hello, world</project><date>2020-08-15</date>
      

      【讨论】:

        【解决方案3】:

        每种方法都可能有问题。有些方法也会删除有用的空格,有些方法在节点有属性时会变得麻烦。所以我会给你第三种方法。这也可能是一种不完美的方法:)

        from simplified_scrapy import SimplifiedDoc,utils
        # xml_doc = utils.getFileContent('myfile.xml')
        xml_doc = """<?xml version="1.0" encoding="UTF-8"?>
         <data>
         <head>
             <version>1.0</version>
             <project>hello, world</project>
             <date>2020-08-15</date>
         </head>
         <file name="helloworld.py"/>
         <file name="helloworld.ps1"/>
         <file name="helloworld.bat"/>
        </data>"""
        
        doc = SimplifiedDoc(xml_doc)
        headXml = doc.head.html.strip() # Get internal data of head
        print (doc.replaceReg(headXml,'>[\s]+<','><')) # Replace newlines and spaces with regex
        

        结果:

        <version>1.0</version><project>hello, world</project><date>2020-08-15</date>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多