xmltodict icon indicating copy to clipboard operation
xmltodict copied to clipboard

Type casting?

Open DougPlumley opened this issue 5 years ago • 3 comments

Curious if there's a good way to "preserve" the text type, for example keep numbers as ints/floats/etc. I was using this to convert XML to a format I can bring into MongoDB but all the numbers come in as strings which makes it difficult to search in MongoDB.

Thanks for the module, works slick!

DougPlumley avatar Mar 15 '19 02:03 DougPlumley

Hi @DougPlumley I think what you want is possible thanks to postprocessor param:

https://github.com/martinblech/xmltodict/blob/master/xmltodict.py#L235

apallier avatar Mar 15 '19 17:03 apallier

hi @apallier could you please help me with how to pass this postprocessor param so that data type remain as it is.

nitingarg1234 avatar Mar 04 '20 07:03 nitingarg1234

Late to the party, but if it helps anyone here is what I did using AST. Note that it will also cast any string that can be interpreted as a Python type, such as lists, tuples, dicts, sets, etc.

import xmltodict
import ast

def auto_cast_str(val):
  # Try fails if cannot eval, therefore is string
  try:
    val = ast.literal_eval(val)
  except:
    pass
  return val

def xml_postprocessor(path, key, value):
  # XML standard requires lower case bools
  if value == "true": value = "True"
  if value == "false": value = "False"
  return key, auto_cast_str(value)

xml_dict = xmltodict.parse(xml_file.read(), postprocessor=xml_postprocessor)

Louis-DR avatar Sep 18 '22 13:09 Louis-DR