我有一個帶有結構的 MyXml.xml:
<?xml version='1.0' encoding='utf-8'?>
<tag1 atrib1='bla' atrib1='bla' atrib1='bla' atrib1='bla'>
<tag2 atrib = 'something'>
<tag3 atrib = 'something'>
<tag4 atrib = '..'>
</tag4>
</tag3>
<tag5 atrib = 'important'><div><h1>ContentFrom **OldXml.xml** </h1></div>
...
</tag5>
</tag2>
</tag1>
有誰知道如何使它成為這種形式(洗掉所有空格):
<?xml version='1.0' encoding='utf-8'?>
<tag1 atrib1='bla' atrib1='bla' atrib1='bla' atrib1='bla'>
<tag2 atrib = 'something'>
<tag3 atrib = 'something'>
<tag4 atrib = '..'>
<tag5 atrib = 'important'><div><h1>ContentFrom **OldXml.xml** </h1></div>
...
我試過這個,但不會作業:
# Read in the file to a DOM data structure.
original_document = minidom.parse("MyXml.xml")
# Open a UTF-8 encoded file, because it's fairly standard for XML.
stripped_file = codecs.open("New_MyXml.xml", "w", encoding="utf8")
# Tell minidom to format the child text nodes without any extra whitespace.
original_document.writexml(stripped_file, indent="", addindent="", newl="")
stripped_file.close()
編輯:
檔案是通過 FOR 回圈創建的,在該回圈中創建元素,最后是這樣寫的:
dom = xml.dom.minidom.parseString(ET.tostring(root))
xml_string = dom.toprettyxml()
part1, part2 = xml_string.split('?>')
with open("MyXml.xml", 'w') as xfile:
xfile.write(part1 'encoding=\"{}\"?>\n'.format(m_encoding) part2)
xfile.close()
編輯在一行中列印整個檔案的最新代碼:
dom = xml.dom.minidom.parseString(ET.tostring(root))
xml_string = dom.toxml()
part1, part2 = xml_string.split('?>')
xmlstring = f'{part1} encoding="{m_encoding}"?>\n {part2}'
with open("MyXml.xml", 'w') as xfile:
for line in xmlstring.split("\n"):
xfile.write(line.strip() "\n")
uj5u.com熱心網友回復:
如果您實際上只是想去除空格,則根本不需要(或不需要)xml 決議器:
from pathlib import Path
inf = Path("my-input.xml")
with inf.open() as f, inf.with_name(f"stripped-{inf.name}").open("w") as g:
for line in f:
g.write(line.strip() "\n")
Pathlib只是在玩的角色os.path,open等在這里:如果你碰巧不喜歡它,你可以沒有它重寫(但pathlib就是這樣大大優于改寫(munging)文本字串的路徑,我相信你不會想...)
如果您確實需要使用決議器加載,請在撰寫時使用完全相同的技巧,但逐行迭代決議器物件。
示范:
from tempfile import TemporaryFile
data = """<?xml version='1.0' encoding='utf-8'?>
<tag1 atrib1='bla' atrib1='bla' atrib1='bla' atrib1='bla'>
<tag2 atrib = 'something'>
<tag3 atrib = 'something'>
<tag4 atrib = '..'>
</tag4>
</tag3>
<tag5 atrib = 'important'><div><h1>ContentFrom **OldXml.xml** </h1></div>
...
</tag5>
</tag2>
</tag1>"""
with TemporaryFile(mode="w ") as f, TemporaryFile(mode="w ") as g:
f.write(data)
f.seek(0)
print("Before:")
for line in f:
print(line, end="")
g.write(line.strip() "\n")
print("\n\nAfter:")
g.seek(0)
for line in g:
print(line, end="")
編輯:
在您的情況下,有一個更簡單的解決方案:根本不使用(更新:顯然,渲染時根本沒有換行符)。但即使沒有它,我們也可以做同樣的事情:toprettyxml,使用toxml.
xml_string = dom.toprettyxml()
part1, part2 = xml_string.split('?>')
xmlstring = f'{part1} encoding="{m_encoding}"?>\n {part2}'
with open("MyXml.xml", 'w') as xfile:
for line in xmlstring.split("\n"):
xfile.write(line.strip() "\n")
但是我懷疑toprettyxml(indent="")會做同樣的事情:
xml_string = dom.toprettyxml(indent="")
...
with open("MyFile.xml", "w") as f:
f.write(xml_string)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314402.html
