gpt4 book ai didi

python - 使用 ElementTree 强制对错误的 XML 文件进行编码

转载 作者:太空宇宙 更新时间:2023-11-04 01:40:39 25 4
gpt4 key购买 nike

大量 XML 文件定义了错误的编码。它应该是 utf-8,但内容到处都是 latin-1 字符。解析此内容的最佳方式是什么?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

编辑:这发生在 Adob​​e InDesign IDML 文件中,似乎“内容”文本具有 latin-1,但其余部分可能是 utf-8。我赞成使用 utf-8 进行正常解析,然后将 Content 中的 Unicode 文本 block 重新编码为 utf-8,然后使用 latin-1 重新进行解析。真是一团糟。ಠ_ಠ

最佳答案

您可以在解析 XML 时覆盖指定的编码:

class xml.etree.ElementTree.XMLParser(html=0, target=None, encoding=None)

Element structure builder for XML source data, based on the expat parser. html are predefined HTML entities. This flag is not supported by the current implementation. target is the target object. If omitted, the builder uses an instance of the standard TreeBuilder class. encoding 1 is optional. If given, the value overrides the encoding specified in the XML file.

docs

关于python - 使用 ElementTree 强制对错误的 XML 文件进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5275325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com