本文介紹了XML 聲明獨(dú)立=“是";lxml的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我正在解析一個(gè) xml,進(jìn)行一些更改并保存到一個(gè)新文件中.它有我想保留的聲明 <?xml version="1.0" encoding="utf-8" Standalone="yes"?>
.當(dāng)我保存新文件時(shí),我丟失了 standalone="yes"
位.我怎樣才能把它留在里面?這是我的代碼:
I have an xml I am parsing, making some changes and saving out to a new file. It has the declaration <?xml version="1.0" encoding="utf-8" standalone="yes"?>
which I would like to keep. When I am saving out my new file I am loosing the standalone="yes"
bit. How can I keep it in?
Here is my code:
templateXml = """<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<package>
<provider>Some Data</provider>
<studio_display_name>Some Other Data</studio_display_name>
</package>"""
from lxml import etree
tree = etree.fromstring(templateXml)
xmlFileOut = '/Users/User1/Desktop/Python/Done.xml'
with open(xmlFileOut, "w") as f:
f.write(etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8'))
推薦答案
您可以將 standalone
關(guān)鍵字參數(shù)傳遞給 tostring()
:
You can pass standalone
keyword argument to tostring()
:
etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone=True)
這篇關(guān)于XML 聲明獨(dú)立=“是";lxml的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!