Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 应用元素树解析复杂的xml结构_Python_Xml_Elementtree - Fatal编程技术网

Python 应用元素树解析复杂的xml结构

Python 应用元素树解析复杂的xml结构,python,xml,elementtree,Python,Xml,Elementtree,我在解析下面的xml文件时遇到问题。以下是我尝试过的 <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <corpus name="P4P" version="1.0" lng="en" xmlns="http://clic.ub.edu/mbertran/formats/paraphrase-corpus" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

我在解析下面的xml文件时遇到问题。以下是我尝试过的

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<corpus name="P4P" version="1.0" lng="en" xmlns="http://clic.ub.edu/mbertran/formats/paraphrase-corpus"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://clic.ub.edu/mbertran/
formats/paraphrase-corpus http://clic.ub.edu/mbertran/formats/paraphrase-corpus.xsd">
    <snippets>
        <snippet id="16488"    source_description="type:plagiarism;plagiarism_reference:00061;
        offset:47727;length:182;source:P4P;wd_count:37">
        All art is imitation of nature.
        </snippet>

    </snippets>
</corpus>
我的输出是空的

我的期望输出:

"type:plagiarism;plagiarism_reference:00061;
        offset:47727;length:182;source:P4P;wd_count:37"
所有的艺术都是对自然的模仿。


非常感谢您的建议。

您需要在元素前面加上xml名称空间。如果在解析后打印root,您将得到

   <Element '{http://clic.ub.edu/mbertran/formats/paraphrase-corpus}corpus' at 0x7ff7891f6390>
            ^       this part here is the full name                       ^

您可以阅读有关处理名称空间的内容@

我不认为您的输出对于发布的代码是空的
snippet\snippet
至少会引起一个错误。@MadPhysicator,对不起,我把斜杠放错了,我现在就编辑这个问题。但这就是我得到的结果。很好的解决方案,它奏效了。非常感谢。
   <Element '{http://clic.ub.edu/mbertran/formats/paraphrase-corpus}corpus' at 0x7ff7891f6390>
            ^       this part here is the full name                       ^
for snippets in root.findall('{http://clic.ub.edu/mbertran/formats/paraphrase-corpus}snippets'):
    for s in snippets.findall('{http://clic.ub.edu/mbertran/formats/paraphrase-corpus}snippet'):
        print s.get('source_description')