我是python的新手,想使用字典值编写XML文件。
当我将字典分配给变量并按如下所述使用dicttoxml时,我可以编写xml文件。
My_Dict = {'data': [{'entity_id': '123456',
'layout': [{u'database': {u'id': abc
u'name': u'aaa abc
u'section': {u'id': u'abc123',
u'name': u'MYSTRING'},
u'seriesCode': u'SR11111',
u'table': {u'id': u'TB98765',
u'name': u'STRING \u1111 STRING\u2000'},
u'topic': {u'id': u'TP890762',
u'name': u'Zee-STRING(Name)'}}],
'metadata': {'classification': ONE,
'country': {'id': 'YYY', 'name': 'ABC'},
'end_date': datetime.date(2019, 11, 02),
'frequency': {'id': 'YYYY', 'name': 'string'},
'id': 123456,
'indicator': one,
'key_series': one
'last_change': {'is_infinity': one
'is_opposite': one
'value': -87},
'last_update_time': datetime.datetime(2000, 05, 12, 10, 8, 46, tzinfo=tzutc()),
'last_value': 77777777,
'multiplier_code': 'OP',
'name': 'Abc Defg',
'new_series': one,
'number_of_observations': 19,
'period_end': 00,
'province': None,
'source': {'country_id': one,
'id': 'YYY-ABC
'name': 'ABC DEF IGK'},
'start_date': datetime.date(1983, 20, 05),
'status': {'id': 'K', 'name': 'Inactive'},
'unit': {'id': '200056', 'name': 'PQR pp'}},
'subscribed': one,
'time_points': one,
'ui': one}
dicttoxml.dicttoxml( My_Dict , attr_type=False, root=True)
但是当我执行另一个SDK文件时,上面的字典值将生成,并且它的类型不是dict。生成值的类型为
<class 'FileName.FolderName.Classname.FucntionName'>
如果我将结果转换为str并尝试将str-> dic转换为
xml_data = dicttoxml.dicttoxml( str(My_dict), attr_type=False, root=True )
我遇到以下错误。
Traceback (most recent call last):
xml_data = dicttoxml.dicttoxml( str(xmltext), attr_type=False, root=True )
File "C:\Python27\lib\site-packages\dicttoxml.py", line 393, in dicttoxml
convert(obj, ids, attr_type, item_func, cdata, parent=custom_root),
File "C:\Python27\lib\site-packages\dicttoxml.py", line 177, in convert
return convert_kv(item_name, obj, attr_type, cdata)
File "C:\Python27\lib\site-packages\dicttoxml.py", line 334, in convert_kv
attrstring = make_attrstring(attr)
File "C:\Python27\lib\site-packages\dicttoxml.py", line 117, in make_attrstring
attrstring = ' '.join(['%s="%s"' % (k, v) for k, v in attr.items()])
AttributeError: 'bool' object has no attribute 'items'
请帮助我,如何解决此错误。