xml解析问题编码

2024-09-20 23:00:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个这样的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<tw>
    <tweet>
        <yazi>atılacak tweet 1</yazi>
        <resim>resim.png</resim>
    </tweet>
    <tweet>
        <yazi>atılacak tweet 2</yazi>
        <resim>yok</resim>
    </tweet>
</tw>

我试着用它来读

^{pr2}$

但我发现这个错误

xml.etree.ElementTree.ParseError: encoding specified in XML declaration is incorrect: line 1, column 31

为什么?如何解决这个问题,我搜索了很多,xml文件看起来没问题。我不明白为什么我看不懂文件。在


Tags: 文件pngversion错误xmlatutfencoding
2条回答

文件中可能有一个无效的utf-8字符,例如xml文件是iso-8859-1编码的。。。或者你可以用utf-8代替utf-8

# -*- coding: utf-8 -*-

import xml.etree.ElementTree as ett


e = ett.parse("tweet.xml").getroot()

print(e.tag)

for altindakiler in e:
    print(altindakiler.tag, altindakiler.attrib)

相关问题 更多 >

    热门问题