martinblech / xmltodict

Python module that makes working with XML feel like you are working with JSON
MIT License
5.47k stars 463 forks source link

Type casting? #209

Open DougPlumley opened 5 years ago

DougPlumley commented 5 years ago

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!

apallier commented 5 years ago

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

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

nitingarg1234 commented 4 years ago

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

Louis-DR commented 2 years ago

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)