Python | Converting String content to dictionary
Sometimes, we are fed with string and we may have to convert it’s content to dictionary. The string may have a specified format that could be key-value convertible. This type of problem is quite common in Machine Learning domain. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using split() + dictionary comprehension
The combination of above method can be used to perform this particular task. This is 2 process method. In 1st step, the string is converted to list using split and then converted back to dictionary using dictionary comprehension.
# Python3 code to demonstrate working of # Converting String content to dictionary # Using dictionary comprehension + split() # initializing string test_str = "Gfg = 1, Good = 2, CS = 3, Portal = 4" # printing original string print("The original string is : " + test_str) # Using dictionary comprehension + split() # Converting String content to dictionary res = {key: int(val) for key, val in (item.split('=') for item in test_str.split(', '))} # printing result print("The newly created dictionary : " + str(res)) |
The original string is : Gfg = 1, Good = 2, CS = 3, Portal = 4
The newly created dictionary : {' CS ': 3, 'Gfg ': 1, ' Portal ': 4, ' Good ': 2}
Method #2 : Using eval()
This particular problem can be solved using the inbuilt function eval which internally evaluates the string and converts the string to dictionary depending upon the condition.
# Python3 code to demonstrate working of # Converting String content to dictionary # Using eval() # initializing string test_str = "Gfg = 1, Good = 2, CS = 3, Portal = 4" # printing original string print("The original string is : " + test_str) # Using eval() # Converting String content to dictionary res = eval('dict(% s)' % test_str) # printing result print("The newly created dictionary : " + str(res)) |
Recommended Posts:
- Python | Converting list string to dictionary
- Python | Convert string dictionary to dictionary
- Python | Convert dictionary object into string
- Python program to create a dictionary from a string
- Find the first repeated word in a string in Python using Dictionary
- Python | Convert byteString key:value pair of dictionary to String
- Python Dictionary to find mirror characters in a string
- Python | Convert key-value pair comma separated string into dictionary
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python | Converting all strings in list to integers
- Python __iter__() and __next__() | Converting an object into an iterator
- Python Dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



