使用xmltodict python解析XML文档时出错

2024-09-30 08:29:29 发布

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

我复制了下面一个名为“dictionary.XML”的XML文档,由于某些原因,该文档无法用python中的XmlToDict进行解析

<dictmap>

<lexelt item="begin.v" num="4">
<sense id="1" wordnet="2,3,5,6,7" gloss="start, have an initial point " examples="Prices for these homes begin at $250,000 | The war began on a Thursday. | She began to feel sick. | When life began was there oxygen in the atmosphere? | The novel begins with a shipwreck at sea. | It's beginning to look like rain. | His property begins at the fence." />
<sense id="2" wordnet="1,8,5" gloss="initiate an undertaking" examples="They'll begin their tour with a concert in London. | You should begin the stew now so it will have four hours to simmer. | The government is beginning to protect the coral reefs of that island. | He was just beginning a novel when the phone rang. | Acme Tire Co. began offering their employees health insurance. | John has begun to take them seriously. | She began ballet at age four." />
<sense id="3" wordnet="4" gloss="make a locution, speak" examples=""now listen, friends", he began." />
<sense id="4" wordnet="9" gloss="partially attain" examples="The rent you could get for that place wouldn't begin to cover the mortgage and taxes. | Can those refugees even begin to hope they'll be granted asylum? | I couldn't begin to tell you all the ways she has contributed to this club." />
</lexelt>

<lexelt item="complain.v" num="2">
<sense id="1" wordnet="1" gloss="express feelings of pain, dissatisfaction, or resentment" examples="Lots of people have complained about the noise. | You're always complaining! | He complained that his boss was useless and he had too much work. | She's complaining about a headache." />
<sense id="2" wordnet="2" gloss="make formal, legal accusation or bring charge" examples="The neighbors complained to the police about the noise. | We have complained to the authorities and they will need to take action now. | The lawyer complained that the defendant had physically abused his client." />
</lexelt>


</dictmap>

以下代码应该可以工作,但正在触发以下错误:

xmltodict.parse('dictionary.xml')

>>>>
---------------------------------------------------------------------------
ExpatError                                Traceback (most recent call last)
<ipython-input-16-c098f9f722f0> in <module>
----> 1 xmltodict.parse('dictionary.xml')

~/opt/anaconda3/lib/python3.7/site-packages/xmltodict.py in parse(xml_input, encoding, expat, process_namespaces, namespace_separator, disable_entities, **kwargs)
    325         parser.ParseFile(xml_input)
    326     else:
--> 327         parser.Parse(xml_input, True)
    328     return handler.item
    329 

ExpatError: syntax error: line 1, column 0

我需要做什么来纠正这个问题


Tags: thetoidhavexmlexampleswordnetat

热门问题