0

我正在尝试将 xml 数据转换为 python 字典数据。xmltodict 在我的代码中运行良好。

以下是示例:

  myxml = """
 <mydocument has="an attribute">
   <and>
     <many>elements</many>
     <many>more elements</many>
   </and>
   <plus a="complex">
     element as well
   </plus>
 </mydocument>
 """

如果我写data = xmltodict.parse(myxml)那么它工作正常。但是我的 xml 比这大得多,我想用plus将所有标记名重命名为children。然后输出字典将包含子键而不是加键。任何建议请...

4

2 回答 2

0

您可以<plus></plus>先使用类似的库重命名标签ElementTree,然后再转换为 dict。这是代码:

import xml.etree.ElementTree as ET
import xmltodict
import json

myxml = """
 <mydocument has="an attribute">
   <and>
     <many>elements</many>
     <many>more elements</many>
   </and>
   <plus a="complex">
     element as well
   </plus>
 </mydocument>
 """
#rename tag
root = ET.fromstring(myxml)
for elem in root.iter('plus'):
    elem.tag = 'children'

newxml = ET.tostring(root, encoding='utf8', method='xml')
xml_dict = dict(xmltodict.parse(newxml)) #convert to Ordered dict and then a normal dict(optional, OrderedDict is returned by default if only using xmltodict)
print(json.dumps(xml_dict, indent=4)) #pretty print to view dict tree(optional)
#Output:
 {
    "mydocument": {
        "@has": "an attribute",
        "and": {
            "many": [
                "elements",
                "more elements"
            ]
        },
        "children": {
            "@a": "complex",
            "#text": "element as well"
        }
    }
}
于 2019-01-31T08:13:25.440 回答
0

正如我上面评论的那样,我建议将字符串替换pluschildren使用模式匹配。

import re

myxml = """
 <mydocument has="an attribute">
   <and>
     <many>elements</many>
     <many>more elements</many>
   </and>
   <plus a="complex">
     element as well
   </plus>
 </mydocument>
 """

myxml = re.sub('(?<=</)?plus', 'children', myxml)
print(myxml)

结果是

 <mydocument has="an attribute">
   <and>
     <many>elements</many>
     <many>more elements</many>
   </and>
   <children a="complex">
     element as well
   </children>
 </mydocument>
于 2019-01-31T08:18:58.767 回答