Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substitute spaces for underscore in dictionary keys with spaces #12

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions dicttoxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ def convert_dict(obj, ids, parent):
addline(convert_bool(k, v, attr))
elif isinstance(v, dict):
addline('<%s type="dict"%s>%s</%s>' % (
k, make_attrstring(attr), convert_dict(v, ids, k), k)
k.replace(" ", "_"), make_attrstring(attr), convert_dict(v, ids, k), k.replace(" ", "_"))
)
elif type(v) in (list, set, tuple) or isinstance(v, collections.Iterable):
addline('<%s type="list"%s>%s</%s>' % (
k, make_attrstring(attr), convert_list(v, ids, k), k)
k.replace(" ", "_"), make_attrstring(attr), convert_list(v, ids, k), k.replace(" ", "_"))
)
elif v is None:
addline('<%s type="null"%s></%s>' % (k, make_attrstring(attr), k))
addline('<%s type="null"%s></%s>' % (k.replace(" ", "_"), make_attrstring(attr), k.replace(" ", "_")))
else:
raise TypeError('Unsupported data type: %s (%s)' % (obj, type(obj).__name__))
return ''.join(output)
Expand Down Expand Up @@ -143,15 +143,15 @@ def convert_kv(key, val, attr={}):
logging.info('Inside convert_kv(): k=%s, type(v) is: %s' % (key, type(val).__name__))
attrstring = make_attrstring(attr)
return '<%s type="%s"%s>%s</%s>' % (
key, type(val).__name__ if type(val).__name__ != 'unicode' else 'str',
attrstring, xml_escape(val), key
key.replace(" ", "_"), type(val).__name__ if type(val).__name__ != 'unicode' else 'str',
attrstring, xml_escape(val), key.replace(" ", "_")
)

def convert_bool(k, v, attr={}):
"""Converts a boolean into an XML element"""
logging.info('Inside convert_bool(): k=%s, type(v) is: %s' % (k, type(v).__name__))
attrstring = make_attrstring(attr)
return '<%s type="bool"%s>%s</%s>' % (k, attrstring, unicode(v).lower(), k)
return '<%s type="bool"%s>%s</%s>' % (k.replace(" ", "_"), attrstring, unicode(v).lower(), k.replace(" ", "_"))

def dicttoxml(obj, root=True, ids=False):
"""Converts a python object into XML"""
Expand All @@ -164,3 +164,4 @@ def dicttoxml(obj, root=True, ids=False):
else:
addline(convert(obj, ids, parent=''))
return ''.join(output)