![](/img/trans.png)
[英]Python and SQLAlchemy Printing Database Contents Using For Loop
[英]printing contents in xml using python
我正在嘗試使用以下腳本將xml轉換為xslt。 轉換完美無缺。 現在,我想將內容寫入新的xml文件中。 當我嘗試這樣做時,我的xml文件中只有一行。 但是,當我在控制台中打印它時,會得到多行輸出。 誰能發現我做錯了什么?
import lxml.etree as ET
dom = ET.parse("1.xml")
xslt = ET.parse("1.xsl")
transform = ET.XSLT(xslt)
newdom = transform(dom)
print(newdom)
newdom.write("output.xml")
這解決了我的問題
with open('output.xml', 'wb') as f:
f.write(newdom)
看來您已經將所有內容寫入“ output.xml”文件。 隨時發布輸出文件以及輸入文件的摘要。
如果唯一的問題是您希望跨多行進行結構化(而不是單行),則只需將漂亮的打印標志設置為true。
newdom.write("output.xml", pretty_print=True)
輸出示例:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td>.</td>
<td>.</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.