l = ['Component 1: Y component: Quantization table 0, Sampling factors 1 horiz/1 vert', 'Component 2: Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert', 'Component 3: Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert', 'Compression Type: Progressive, Huffman', 'Content-Length: 14312', 'Content-Type: image/jpeg'] d = {} for ele in l: spl = ele.split(":", 2) if len(spl) == 3: k1, k2, v = spl d[k1] = {k2: v.split(",")} else: k,v = spl d[k] = v.split() if "," in v else v
Output:
{'Component 1': {' Y component': [' Quantization table 0', ' Sampling factors 1 horiz/1 vert']}, 'Component 2': {' Cb component': [' Quantization table 1', ' Sampling factors 1 horiz/1 vert']}, 'Component 3': {' Cr component': [' Quantization table 1', ' Sampling factors 1 horiz/1 vert']}, 'Compression Type': [' Progressive', ' Huffman'], 'Content-Length': ' 14312', 'Content-Type': ' image/jpeg'}
To remove a space, you can str.strip disable it:
d = {} for ele in l: spl = ele.split(":", 2) if len(spl) == 3: k1, k2, v = spl d[k1] = {k2.strip(): list(map(str.strip,v.split(",")))} else: k,v = spl d[k] = list(map(str.strip, v.split())) if "," in v else v.strip
Output:
{'Component 1': {'Y component': ['Quantization table 0', 'Sampling factors 1 horiz/1 vert']}, 'Component 2': {'Cb component': ['Quantization table 1', 'Sampling factors 1 horiz/1 vert']}, 'Component 3': {'Cr component': ['Quantization table 1', 'Sampling factors 1 horiz/1 vert']}, 'Compression Type': ['Progressive', 'Huffman'], 'Content-Length': '14312', 'Content-Type': 'image/jpeg'}
Both of them really correspond to the expected result.