I have a dictionary containing the character positions of different fields in a string. I’d like to use that information to slice the string. I’m not really sure how to best explain this, but the example should make it clear:
input:
mappings = {'name': (0,4), 'job': (4,11), 'color': (11, 15)}
data = "JohnChemistBlue"
desired output:
{'name': 'John', 'job': 'Chemist', 'color': 'Blue'}
Please disregard the fact that jobs, colors and names obviously vary in character length. I’m parsing fixed-length fields but simplified it here for illustrative purposes.
or with a generator instead of a list (probably more efficient):