- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 LXML 的新手,在解析我的元素后遇到问题:如果我删除(或替换)最后一个子元素,它的体系结构似乎已经改变。这是我的代码。
(抱歉,我是 stackoverflow 的新手,所以我无法发布图片)
我已经查找了解决方案,但我仍然无法确定我做错了什么。我真的很感激有人的帮助!(我在 Windows 上使用 LXML 3.2.1 和 Python 2.6)
from lxml import etree
from copy import deepcopy
def Write( file, element ):
f = open( file, 'w' )
f.write( etree.tostring( element, xml_declaration=True, encoding="ISO-8859-1", pretty_print = True ) )
f.close()
return 1
def ReadAndReturn( file ):
lookup = etree.ElementDefaultClassLookup()
parser = etree.XMLParser(recover = True)
parser.set_element_class_lookup( lookup )
mainTree = etree.parse( file, parser )
return mainTree
# create a root element with 3 children
root = etree.Element( "root" )
root.append( etree.Element( "child1" ) )
child2 = etree.SubElement( root, "child2" )
child2.text = 'CHILD2'
child3 = etree.SubElement( root, "child3" )
child3.text = 'CHILD3'
print "\n--- INITIAL ROOT ---"
print( etree.tostring( root, pretty_print=True ) )
# remove last child
root2 = deepcopy( root )
root2.remove( root2[2] )
print "--- ROOT WITHOUT LAST CHILD / BEFORE WRITING ---"
print( etree.tostring( root2, pretty_print=True ) )
# write initial root (3 children) and read the file
filename = 'test.tst'
status = Write( filename, root )
tree = ReadAndReturn( filename )
# remove last child from the read element
root3 = deepcopy( tree.getroot() )
root3.remove( root3[2] )
print "--- ROOT WITHOUT LAST CHILD / AFTER WRITING AND PARSING ---"
print( etree.tostring( root3, pretty_print=True ) )
最佳答案
空白处理可能很棘手。这是您的程序的简化版本,它演示了正在发生的事情。
from lxml import etree
# Create a root element with 3 children
root = etree.Element( "root" )
root.append( etree.Element( "child1" ) )
child2 = etree.SubElement( root, "child2" )
child2.text = 'CHILD2'
child3 = etree.SubElement( root, "child3" )
child3.text = 'CHILD3'
# Print the "ugly" XML (no whitespace)
print "\n--- UGLY ---"
print etree.tostring(root)
# Print the "pretty" XML
print "\n--- PRETTY ---"
pp = etree.tostring(root, pretty_print=True)
print pp
# Parse the pretty XML
tree = etree.fromstring(pp)
# remove last child
tree.remove(tree[2])
print "--- WITHOUT LAST CHILD PART 1 ---"
print etree.tostring(tree, pretty_print=True)
# Parse the pretty XML once again with parser option 'remove_blank_text=True'
tree = etree.fromstring(pp, etree.XMLParser(remove_blank_text=True))
# remove last child
tree.remove(tree[2])
print "--- WITHOUT LAST CHILD PART 2 ---"
print etree.tostring(tree, pretty_print=True)
输出:
--- UGLY ---
<root><child1/><child2>CHILD2</child2><child3>CHILD3</child3></root>
--- PRETTY ---
<root>
<child1/>
<child2>CHILD2</child2>
<child3>CHILD3</child3>
</root>
--- WITHOUT LAST CHILD PART 1 ---
<root>
<child1/>
<child2>CHILD2</child2>
</root>
--- WITHOUT LAST CHILD PART 2 ---
<root>
<child1/>
<child2>CHILD2</child2>
</root>
child2
pretty-print 的 XML 文档中的元素有一个 .tail
由 \n
组成的属性(property)后跟两个空格。您可以通过 repr(pp)
查看.这两个空格是导致 </root>
的原因结束标记未对齐。
如果使用解析器选项 remove_blank_text=True
解析 pretty-print 的 XML 文档,那么将不会有干扰性的纯空白元素尾部,最后(“第 2 部分”) pretty-print 将按预期工作。
另见:
关于python - 从 pretty-print 的 XML 中删除元素后留下不需要的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16565966/
目前,我在单击我的正文时打开一个新选项卡,但它将焦点更改为子窗口。但我想通过留下窗口来做到这一点,这样弹出窗口就不会被阻止,并且我可以将焦点放在当前窗口上。 以下代码当前正在运行 /** * For
我有一个带有线性渐变的 css 背景图像。 CSS 代码: .footer-about { display: flex; align-items: center; justif
在服务中包含以下代码(假设在快速端点上): async function (res, req, next) { const fetch = require('node-fetch'); awa
我正在使用 IImageList 和 SHGetFileInfo 为任何给定路径提取巨型图标。一旦我有了它,然后我使用 DrawIconEx 将 HICON 渲染到 HBITMAP 中,最终使用 GD
我有图表并计算了网络密度,现在我应该只保留具有最大权重的分支(网络密度的前 10%,例如 200 中权重最大的 20 个分支)。我找不到该怎么做? 最佳答案 这个问题有点令人困惑,因此,如果稍后出现更
我一直在尝试使用 jquery 创建一种效果,当您将鼠标移到一个 div 上时,整个 body 都会移动,并沿着它经过的点留下一条轨迹。我创建了一个可以使整个 body 移动的功能,但我找不到离开轨迹
我想在桌面上显示放大和缩小图标(控件),而仅在移动设备上浏览时显示 gps 图标。我正在使用这个 css https://unpkg.com/leaflet@1.1.0/dist/leaflet.cs
相关代码(索引为数组大小): typedef struct elemento { unsigned long linha; unsigned long coluna; doub
我有一个 div,因此当我单击时,它会切换为展开或缩回。它在所有浏览器上都运行良好,尽管有一点让我很感兴趣。在谷歌浏览器上,当它缩回时,它会在运动中留下细线。 www.rezoluz.com/logi
每当我的应用程序尝试通过调用 CreateDIBSection() 或使用 LR_CREATEDIBSECTION 标志调用 LoadImage() 来创建 DIB 部分时,它似乎都会成功返回。它返回
我想在 Protractor 测试中脱离 Selenium 控制流。 以下是我迄今为止遇到的步骤和问题: 1。逐个测试禁用它 我的第一个想法是使用 SELENIUM_PROMISE_MANAGER 以
我正在构建一个部署在 CentOS 7.2 上的 ASP.Net Core (netcore 1.1) 应用程序。 我有一个通过 System.Diagnostics.Process 调用外部进程(也
我正在为我的网站创建一个聊天小部件。用户将能够输入纯文本 - 没有 html。 为了消除 HTML 标记并允许用户使用“”,我正在接受他们的输入并在用户屏幕的输入上使用 strip_tags() 和输
我是一名优秀的程序员,十分优秀!