text to dictionary python
Method 1: # Python3 code to demonstrate # convert dictionary string to dictionary # using ast.literal_eval() import ast # initializing string test_string = '{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}' # printing original string print("The original string : " + str(test_string)) # using ast.literal_eval() # convert dictionary string to dictionary res = ast.literal_eval(test_string) # print result print("The converted dictionary : " + str(res)) Method 2: # Python3 code to demonstrate # convert dictionary string to dictionary # using json.loads() import json # initializing string test_string = '{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}' # printing original string print("The original string : " + str(test_string)) # using json.loads() # convert dictionary string to dictionary res = json.loads(test_string) # print result print("The converted dictionary : " + str(res))
converting a string to a dictionary in python
>>> import ast >>> ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}") {'muffin': 'lolz', 'foo': 'kitty'}
Source: stackoverflow.com
convert response text to dictionary python
import requests # Making a get request response = requests.get('https://api.github.com') # printing request text print(response.text)
Source: www.geeksforgeeks.org