0

我在一个 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
4

1 回答 1

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

>>> 
于 2012-11-05T08:50:49.457 回答