我有一个创建xml文档的小脚本,并使用doc = lxml.etree.SubElement(root, 'dependencies')
for depen in dependency_list:
dependency = lxml.etree.SubElement(doc, 'dependency')
lxml.etree.SubElement(dependency, 'groupId').text = depen.group_id
lxml.etree.SubElement(dependency, 'artifactId').text = depen.artifact_id
lxml.etree.SubElement(dependency, 'version').text = depen.version
if depen.scope == 'provided' or depen.scope == 'test':
lxml.etree.SubElement(dependency, 'scope').text = depen.scope
exclusions = lxml.etree.SubElement(dependency, 'exclusions')
exclusion = lxml.etree.SubElement(exclusions, 'exclusion')
lxml.etree.SubElement(exclusion, 'groupId').text = '*'
lxml.etree.SubElement(exclusion, 'artifactId').text = '*'
tree.write('explicit-pom.xml' , pretty_print=True)
生成格式正确的xml文档。但是,选项卡缩进是2个空格,我想知道是否有办法将其更改为4个空格(我认为它看起来更好4个空格)。有没有一种简单的方法来实现它?
代码段:
{{1}}
答案 0 :(得分:4)
如果有人仍在尝试实现此目标,则可以使用lxml 4.5中的cond = df1['A'] >= df1['A'].shift(-1) + 4
方法来完成-
etree.indent()
答案 1 :(得分:1)
python lxml
API似乎无法做到这一点。
标签间距的可能解决方案是:
def prettyPrint(someRootNode):
lines = lxml.etree.tostring(someRootNode, encoding="utf-8", pretty_print=True).decode("utf-8").split("\n")
for i in range(len(lines)):
line = lines[i]
outLine = ""
for j in range(0, len(line), 2):
if line[j:j + 2] == " ":
outLine += "\t"
else:
outLine += line[j:]
break
lines[i] = outLine
return "\n".join(lines)
请注意,这不是很有效。只有在lxml
C代码中本机实现此功能时,才能实现高效率。