用lxml的etree.froms读取xml时的问题:ValueError: Unicode strings with encoding declaration are not supported.

如标题所示,etree.froms(xml)出现如下错误:

ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.

还以为是xml文件的格式错误,结果改了还是报错,
看到了这篇文章:https://blog.csdn.net/suiyuan2009/article/details/104097491

在 xml后面加上 **.encode(‘utf-8’) ** 就解决问题了

xml = etree.fromstring(xml_str.encode('utf-8'))

猜你喜欢

转载自blog.csdn.net/qq_38828370/article/details/122609073