Python 为什么可以';我读了一个XML示例文件。xml.etree.ElementTree.ParseError:语法错误:第1行第0列

Python 为什么可以';我读了一个XML示例文件。xml.etree.ElementTree.ParseError:语法错误:第1行第0列,python,xml,python-3.x,xml-parsing,Python,Xml,Python 3.x,Xml Parsing,我不熟悉编程和python。我想学习通过Python 3.6修改XML文件。 我创建了一个XML文件,如中所示 示例文件: <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</g

我不熟悉编程和python。我想学习通过Python 3.6修改XML文件。 我创建了一个XML文件,如中所示

示例文件:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
它给了我这个错误:

Traceback (most recent call last):
  File "C:/Program Files/Python36/programele/farmi.py", line 3, in <module>
    tree = ET.parse('C:\Program Files\Python36\programele\country_data.xml')
  File "C:\Program Files\Python36\lib\xml\etree\ElementTree.py", line 1196, in parse
    tree.parse(source, parser)
  File "C:\Program Files\Python36\lib\xml\etree\ElementTree.py", line 597, in parse
    self._root = parser._parse_whole(source)
  File "<string>", line None
xml.etree.ElementTree.ParseError: syntax error: line 1, column 0
回溯(最近一次呼叫最后一次):
文件“C:/Program Files/Python36/programele/farmi.py”,第3行,在
tree=ET.parse('C:\Program Files\Python36\programele\country\u data.xml')
文件“C:\Program Files\Python36\lib\xml\etree\ElementTree.py”,第1196行,解析中
parse(源,解析器)
文件“C:\Program Files\Python36\lib\xml\etree\ElementTree.py”,第597行,解析中
self.\u root=parser.\u parse\u整体(源)
文件“”,第行无
xml.etree.ElementTree.ParseError:语法错误:第1行第0列
我甚至尝试了这个
ET.fromstring(open('country\u data.xml').read())
,但它不起作用

我非常感谢您的建议和信息


谢谢

在我的案例中,我发现xml文件无效。也就是说,该文件没有xml文件结构。我建议您通过在文本编辑器中打开来检查xml文件的内容,这样就可以轻松查看文件结构

但是,如果你正在处理很多XML文件,并且你不知道你的XML文件是否无效,你应该考虑在Python代码中添加一个“尝试/否”块。像这样:

尝试: tree=ET.parse(文件) root=tree.getroot() 除:
pass

在我的案例中,我发现xml文件无效。也就是说,该文件没有xml文件结构。我建议您通过在文本编辑器中打开来检查xml文件的内容,这样就可以轻松查看文件结构

但是,如果你正在处理很多XML文件,并且你不知道你的XML文件是否无效,你应该考虑在Python代码中添加一个“尝试/否”块。像这样:

尝试: tree=ET.parse(文件) root=tree.getroot() 除:
pass

将xml文件包括在您的问题中。我添加了具有相同问题的xml文件。检查该文件是否确实存在-碰巧它不存在-它只是错误地具有不同的扩展名(应该是
.xml
)。在您的问题中包括xml文件。我添加了具有相同问题的xml文件。请检查该文件是否确实存在-碰巧它不存在-它只是错误地具有不同的扩展名(它应该是
.xml
)。
Traceback (most recent call last):
  File "C:/Program Files/Python36/programele/farmi.py", line 3, in <module>
    tree = ET.parse('C:\Program Files\Python36\programele\country_data.xml')
  File "C:\Program Files\Python36\lib\xml\etree\ElementTree.py", line 1196, in parse
    tree.parse(source, parser)
  File "C:\Program Files\Python36\lib\xml\etree\ElementTree.py", line 597, in parse
    self._root = parser._parse_whole(source)
  File "<string>", line None
xml.etree.ElementTree.ParseError: syntax error: line 1, column 0