在输出到outlook时,通过XML文件标签读取数据并插入新行

时间:2012-11-05 05:26:46

标签: python

我在XML文件中有以下数据,我正在使用下面提到的python代码阅读然后将其写入outlook,当它写入outlook我看到所有数据都打印在一行...我想要打印一行接一行,我试图在每行末尾的xml文件中使用“br”标签,围绕“br”和“/ br”包围每一行,但似乎没有任何工作,任何建议? XML

<rel_notes>
    1.)Please move to this build for all further test and development 
    activities .
    2.)Please use this as a basebuild to verify 
    compilation and sanity
    3.)Any CL that nees to be integrated must 
    have a CL
</rel_notes>

Python代码

from xml.etree import cElementTree as etree
tree = etree.parse(file)
Releasenotes = ('\n'.join(elem.text for elem in tree.iter('rel_notes')))

输出

Release notes: 1.)Please move to this build for all further test and development activities 2.)Please use this as a basebuild to verify compilation and sanity 3.)Any CL that nees to be integrated must have a CL

1 个答案:

答案 0 :(得分:0)

我没有使用标准的XML Python库,但lxml更强大(支持XPath等)。看起来它基本上按照你的意愿工作:

>>> s = """<rel_notes>
    1.)Please move to this build for all further test and development 
    activities .
    2.)Please use this as a basebuild to verify 
    compilation and sanity
    3.)Any CL that nees to be integrated must 
    have a CL
</rel_notes>"""
>>> from lxml import etree
>>> root = etree.fromstring(s)
>>> root.text
'\n    1.)Please move to this build for all further test and development \n    activities .\n    2.)Please use this as a basebuild to verify \n    compilation and sanity\n    3.)Any CL that nees to be integrated must \n    have a CL\n'
>>> print root.text

    1.)Please move to this build for all further test and development 
    activities .
    2.)Please use this as a basebuild to verify 
    compilation and sanity
    3.)Any CL that nees to be integrated must 
    have a CL

>>>