【发布时间】:2015-08-28 21:20:28
【问题描述】:
我的 XML(从 .docx 中提取):
<w:p>
<w:pPr>
<w:pStyle w:val="Normal"/>
<w:rPr/>
</w:pPr>
<w:r>
<w:rPr/>
<w:t>0 things and stuff</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="Normal"/>
<w:rPr/>
</w:pPr>
<w:r>
<w:rPr/>
<w:t>1 things and stuff</w:t>
</w:r>
</w:p>
期望的输出:
0 things and stuff
1 things and stuff
实际输出:
0 things and stuff1 things and stuff
我尝试使用 lxml 包,希望他们的 tostring 方法与 pretty_print 相比,会产生比默认 xml 包更好的结果。
在研究这个问题时,我发现在 tostring 中使用 method='text' 会导致所有格式丢失。
我的代码:
tree = etree.fromstring(xml_content)
docx_text = etree.tostring(tree, method='text')
我尝试过使用 pretty_print=True、tostringlist 和 tounicode。我只是在寻找这个包中不存在的功能吗?
【问题讨论】: