python convert dict values to correct data types

Question:

I have a dict of strings:

{'a': '12345', 'b': 'something', 'c': '192847.2134', 'd': '-21374.123', 'e': '-1239', 'f': 'something else'}

The actual values should be a mix of strings, ints, and floats rather than strings:

{'a': 12345, 'b': 'something', 'c': 192847.2134, 'd': -21374.123, 'e': -1239, 'f': 'something else'}

I want to be able to convert them reliably to their respective data types but I’m not sure what the fastest and most reliable way would be. It would be great if I could have some sort of JSON string that maps each key to its data type:

{
  "a": "int",
  "b": "str",
  "c": "float",
  "d": "float",
  "e": "int",
  "f": "str"
}

but I’m not sure if that’s possible, because reading that in will just result in strings rather than the actual data types .

I don’t particularly want to loop over and run a try/except on every key (unless that really is the best way). I saw this, but not sure if it’s the best way or not. Is there something obvious to resolve this?

[edit] This is part of a specification I know ahead of time.

Asked By: crabulus_maximus

||

Answers:

You can do this, Write a mapping function and just iterate through and call corresponding functions.

d = {'a': '12345', 'b': 'something', 'c': '192847.2134', 'd': '-21374.123', 'e': '-1239', 'f': 'something else'} 
mapping = {
  "a": int,
  "b": str,
  "c": float,
  "d": float,
  "e": int,
  "f": str
}
result = {key:mapping.get(key)(value) for key,value in d.items()}

# Out
{'a': 12345,
 'b': 'something',
 'c': 192847.2134,
 'd': -21374.123,
 'e': -1239,
 'f': 'something else'}
Answered By: Rahul K P

You can use literal_eval which will convert strings to their data type like typing them literally in python.

from ast import literal_eval

You can use it to convert the strings and if you need build the dict with types.
Note: that literal_eval supports and only supports native python types, from bytes to dicts.

Answered By: Daraan

If you want to rather infer the types and convert them to what is most appropriate and if your dict looks as your write, ie. one key, one value, you can also use pandas:

import pandas as pd

my_dict = {'a': '12345', 'b': 'something', 'c': '192847.2134', 'd': '-21374.123', 'e': '-1239', 'f': 'something else'}

pd.DataFrame([my_dict]).apply(pd.to_numeric, errors='ignore').convert_dtypes().to_dict(orient='records')[0]

that will give you:

{'a': 12345,
 'b': 'something',
 'c': 192847.2134,
 'd': -21374.123,
 'e': -1239,
 'f': 'something else'}

and if you stopped before converting back to dict and checked the types:

a      Int64
b     string
c    Float64
d    Float64
e      Int64
f     string
dtype: object
Answered By: My Work
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.