xmltodict
xmltodict copied to clipboard
Type casting?
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!
Hi @DougPlumley I think what you want is possible thanks to postprocessor
param:
https://github.com/martinblech/xmltodict/blob/master/xmltodict.py#L235
hi @apallier could you please help me with how to pass this postprocessor param so that data type remain as it is.
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)