For example, input1, input2 variables and key_loc is list of keys for the dictionary,
Input:
input1='sam,geti,1234'
input2='jhon,somwhre,676966'
key_loc =['name','addr','phone']
I’m expecting the output in the below format.
Output:
output={'recod1:{'name':'sam','addr':'geti','phone':'12343'},
'record2':{'name':'jhon','addr':'somewhre','phone':'676966'}}
How can we do this in python?
I’ll assume that you have a file or file-like object
inputcontaining the input lines you posted. Then you can use this code to parse it into a dictionary like this:However, if you don’t actually need the number from
input1etc., consider using alistas the top-level object: