有没有办法让ElementTree.parse()和ElementTree.tostring()不从XML字符串中删除换行符?

我正在使用以下代码(在stackexchange上找到)合并XML文件:

#!/usr/bin/env python
import sys
from xml.etree import ElementTree

def run(files):
    first = None
    for filename in files:
        data = ElementTree.parse(filename).getroot()
        if first is None:
            first = data
        else:
            first.extend(data)
    if first is not None:
        print ElementTree.tostring(first,encoding="utf8")

if __name__ == "__main__":
    run(sys.argv[1:])

但是当字符串在XML中包含换行符时,例如

<item text="blabla line 1

blabla line 3"/>

合并文件的过程将删除换行符。

是否可以通过合并过程合并这些文件,而使换行符保留在原处?我的工作流程中的后续流程会利用它们。

hustsky012 回答:有没有办法让ElementTree.parse()和ElementTree.tostring()不从XML字符串中删除换行符?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2346841.html

大家都在问