第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Python。獲取沒有空格的內(nèi)部 XML

Python。獲取沒有空格的內(nèi)部 XML

九州編程 2023-06-06 15:59:15
我有一個(gè)這樣的 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 元素中獲取數(shù)據(jù),如下所示:<version>1.0</version><project>hello, world</project><date>2020-08-15</date>然后散列它?,F(xiàn)在,我必須進(jìn)行一些字符串操作才能將其合并為一行:root = ET.parse('myfile.xml').getroot()header = ET.tostring(root[0]).decode('utf-8')import reheader = re.sub('\n','',header)header = re.sub('>\s+<','><',header)header = header.replace('<head>','')header = header.replace('</head>','')header = header.strip()有沒有更簡(jiǎn)單的方法來做到這一點(diǎn)?Powershell XML 對(duì)象有一個(gè)簡(jiǎn)單的 InnerXML 屬性,它為您提供一個(gè)元素中沒有空格的 XML 作為字符串。Python 是否有一種方法可以使這更容易?
查看完整描述

3 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊

下面(不使用任何外部庫(kù) - 只是核心 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>


查看完整回答
反對(duì) 回復(fù) 2023-06-06
?
開滿天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊

如果您可以使用外部庫(kù),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 標(biāo)簽的子標(biāo)簽的內(nèi)容,并去除換行符和空格。


查看完整回答
反對(duì) 回復(fù) 2023-06-06
?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊

每種方法都可能有問題。有的方法還會(huì)刪除有用的空格,有的方法在節(jié)點(diǎn)有屬性的時(shí)候就麻煩了。所以我會(huì)給你第三種方法。這也可能是一種不完美的方法:)


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

結(jié)果:


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


查看完整回答
反對(duì) 回復(fù) 2023-06-06
  • 3 回答
  • 0 關(guān)注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)