Search code examples
pythondicttoxml

dicttoxml with number as key


from dicttoxml import dicttoxml

ArrayWithDigitKey={2:"vale"}
xml =dicttoxml(ArrayWithDigitKey)

I get this error :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/dicttoxml.py", line 393, in dicttoxml
    convert(obj, ids, attr_type, item_func, cdata, parent=custom_root), 
  File "/usr/local/lib/python2.7/site-packages/dicttoxml.py", line 189, in convert
    return convert_dict(obj, ids, parent, attr_type, item_func, cdata)
  File "/usr/local/lib/python2.7/site-packages/dicttoxml.py", line 214, in convert_dict
    key, attr = make_valid_xml_name(key, attr)
  File "/usr/local/lib/python2.7/site-packages/dicttoxml.py", line 145, in make_valid_xml_name
    if key.isdigit():
AttributeError: 'int' object has no attribute 'isdigit'

Solution

  • Checking the error once shall give you an idea of what's going wrong.

    The key of 'ArrayWithDigitKey' needs to be an int but in the form of a string.

    As a result, you must define the dictionary as below and it should start working again,

    ArrayWithDigitKey={'2':"vale"}
    

    Let me know if you need any other help.