Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

convert string to dictionary python

# 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))
Comment

poython str to dict

>>> D1={'1':1, '2':2, '3':3}
>>> s=str(D1)
>>> import ast
>>> D2=ast.literal_eval(s)
>>> D2
{'1': 1, '2': 2, '3': 3}

Comment

string to dictionary python

import json
res_dict = json.loads(input_str)
Comment

how to convert string into dictionary python

# Python3 code to demonstrate working of 
# Convert key-value String to dictionary
# Using dict() + generator expression + split() + map()
  
# initializing string
test_str = 'gfg:1, is:2, best:3'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Convert key-value String to dictionary
# Using dict() + generator expression + split() + map()
res = dict(map(str.strip, sub.split(':', 1)) for sub in test_str.split(', ') if ':' in sub)
  
# printing result 
print("The converted dictionary is : " + str(res)) 
Comment

PREVIOUS NEXT
Code Example
Python :: python initialize empty dictionary 
Python :: cannot convert float NaN to integer 
Python :: how to split a string by character in python 
Python :: dockerfile for django project 
Python :: python multiaxis slicing 
Python :: input python 
Python :: python expressions 
Python :: Python remove punctuation from a string 
Python :: box plot seaborn python 
Python :: check anonim user django 
Python :: split word python 
Python :: http server in python 
Python :: how to give autocomplete in python 
Python :: hashing vs encryption vs encoding 
Python :: pygame rotate image 
Python :: sum all values in a matrix python 
Python :: how to generate random numbers in python 
Python :: python pyowm 
Python :: Transform networkx graph to dataframe 
Python :: pyqt menubar example 
Python :: python pandas give column names 
Python :: list comprehension python with condition 
Python :: How to loop over grouped Pandas dataframe? 
Python :: python one line if statement without else 
Python :: pandas select first within groupby 
Python :: how to make addition in python 
Python :: urllib request 
Python :: python factorial 
Python :: run python.py file 
Python :: python find index of first matching element in a list 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =