我正在批量转换许多XML文件,并将其字符编码更改为UTF-8:
with open(source_filename, "rb") as source:
tree = etree.parse(source)
with open(destination_filename, "wb") as destination:
tree.write(destination, encoding="UTF-8", xml_declaration=True)
不幸的是,它破坏了我的
CDATA
部分,而只是转义了它们。来源:
<d><![CDATA[áÌÀøÅàùÑÄéú ëÌÄé áÈàÅùÑ éäå''ä ðÄùÑÀôÌÈè <small><small>(ùí ëå èæ)</small></small>
目标:
<d>בְּרֵאשִׁית כִּי בָאֵשׁ יהו''ה נִשְׁפָּט <small><small>(שם כו טז)</small></small>
我是否可以设置一个设置,告诉我将CDATA部分保留为空?我主要使用LXML更改字符编码并正确编写XML header 。
最佳答案
import lxml.etree as etree
parser = etree.XMLParser(strip_cdata=False)
with open(source_filename, "rb") as source:
tree = etree.parse(source, parser=parser)
关于python - LXML杀死了我的CDATA部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25813756/