Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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 - Fatal编程技术网

用Python解析没有结束标记的XML文件

用Python解析没有结束标记的XML文件,python,xml,Python,Xml,我不太熟悉Python,需要您帮助我导出CSV文件中的XML文件示例: <FORMAT_TITRE_3>2-2 - Texte en application duquel la convention est conclue :</FORMAT_TITRE_3> La procédure de délégation [...]territoriales. <FORMAT_TITRE_3>2-3 - Objet de la délégation :</FOR

我不太熟悉Python,需要您帮助我导出CSV文件中的XML文件示例:

<FORMAT_TITRE_3>2-2 - Texte en application duquel la convention est conclue :</FORMAT_TITRE_3> La procédure de délégation [...]territoriales.
<FORMAT_TITRE_3>2-3 - Objet de la délégation :</FORMAT_TITRE_3>
<TR /> Convention de délégation [...]Fussy.
<TR /> Domaine de la délégation : assainissement collectif
<TR /> Durée de la délégation : xx ans.
<TR /> Lieu principal d'exécution de la délégation : Territoire communal de Fussy.
<TR /> Code Nuts : FR241
<FORMAT_TITRE_3>2-4 - Information sur le montant [...] origine) :</FORMAT_TITRE_3> The contract amounts to : xxx E HT.
<FORMAT_TITRE_3>2-5 - Classification CPV :</FORMAT_TITRE_3> 90110000-1.
<FORMAT_TITRE_3>3 - Procédure :</FORMAT_TITRE_3>

非常感谢您的帮助

什么奇怪的标签?问题是什么?你能举例说明CSV文件应该是什么样子吗?您是否希望保留“领土保护程序”文本?谢谢您的回复@丹尼尔,奇怪的标签指的是那些,反斜杠在右边而不是左边。我的问题如下(如果我不清楚,我很抱歉):我如何解析这样一个XML文件中的所有信息?@nander speerstra,CSV文件应该包括标记之间的文本,如变量名(第1行)和第二行右侧的文本。然而,对于标签后面显示的文本,我认为应该循环使用“2-3-保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区保护区。。。
        # takes as input an xml root, a dictionary where to store the parsed values and an id number suggesting uniqueness of the current node
        def parse_node(root, dict, id):
            # Parse this node
            tag_dict = OrderedDict()
            for key, value in root.attrib.items():
                if id > 1: # if there are more than one childs with the same tag
                    tag_dict[root.tag + str(id) + ':' + key] = value
                else:
                    tag_dict[root.tag + ':' + key] = value
            # Get children of node
            children = root.getchildren()
            # If node has one or more child
            if len(children) >= 1:
                # Loop through all the children
                tag_dict_id = defaultdict(lambda: 0)
                for child in children:
                    tag_dict_id[child.tag] += 1 # keep track of the children
                    # call to recursion function
                    # Parse children
                    parse_node(child, tag_dict, tag_dict_id[child.tag])
            # If does not have children and is the 'search_node'
            elif len(children) == 0:
                # Store the text inside the node.
                if id > 1:
                    tag_dict[root.tag + str(id) + ':text'] = root.text
                else:
                    tag_dict[root.tag + ':text'] = root.text
            # update the current dictionary with the new data
            dict.update(tag_dict)
            return dict