I have a string containing variable names and values. There is no designated separator between the names and the values and the names may or may not contain underscores.
string1 = 'Height_A_B132width_top100.0lengthsimple0.00001'
I would like to get the variables into a dictionary:
# desired output: dict1 = {'Height_A_B': 132, 'width_top': 100.0, 'lengthsimple': 0.00001}
Trying the following itertools method
Input1:
from itertools import groupby
[''.join(g) for _, g in groupby(string1, str.isdigit)]
Output1:
['Height_A_B', '132', 'width_top', '100', '.', '0', 'lengthsimple', '0', '.', '00001']
The following should almost get there, but the iPython interpreter tells me this str attribute doesn't exist (it is in the docs). Anyway...
Input2:
[''.join(g) for _, g in groupby(string1, str.isnumeric)]
Output2:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-25-cf931a137f50> in <module>()
----> 1 [''.join(g) for _, g in groupby(string1, str.isnumeric)]
AttributeError: type object 'str' has no attribute 'isnumeric'
Anyway, what would happen if the number contained an exponent with a '+' or a '-' symbol?
string2 = 'Height_A132width_top100.0lengthsimple1.34e+003'
# desired output: dict2 = {'Height_A_B': 132, 'width_top': 100.0, 'lengthsimple': 1.34e+003}
Input3:
[''.join(g) for _, g in groupby(string2, str.isdigit)]
Output3:
['Height_A', '132', 'width_top', '100', '.', '0', 'lengthsimple', '1', '.', '34', 'e+', '003']
I wonder, if someone has an elegant solution?
UPDATE: There is some discussion below about preserving the types of the numerical variables (e.g. int, float etc.). In fact the scientific notation in string2 turned out to be a bit of a red herring because if you create a variable
>>> a = 1.34e+003
you get
>>> print a
1340.0
anyway, so the chance of producing a string with 1.34+003 in it is low.
So string2 is a more appropriate test case if we change it to, say
string2 = 'Height_A132width_top100.0lengthsimple1.34e+99'