6 examples of 'how to convert string to dictionary in python' in Python

Every line of 'how to convert string to dictionary in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
37def str2dict(dictstr):
38 """
39 @brief Convert string to dictionary
40 """
41 _dict = ast.literal_eval(dictstr)
42 if not isinstance(_dict, dict):
43 _dict = ast.literal_eval(dictstr.replace('"', ''))
44 if not isinstance(_dict, dict):
45 _dict = ast.literal_eval(dictstr.replace("'", ""))
46 if not isinstance(_dict, dict):
47 raise Exception("Cannot convert given string (%s) to dictionary." % (dictstr, ))
48 return _dict
5def dict_conversion(input):
6 try:
7 return literal_eval(input)
8 except Exception:
9 return loads(input)
452def _unicode_convert(obj):
453 """ converts unicode to anscii """
454 if isinstance(obj, dict):
455 return {_unicode_convert(key): \
456 _unicode_convert(value) \
457 for key, value in obj.iteritems()}
458 elif isinstance(obj, list):
459 return [_unicode_convert(element) for element in obj]
460 elif isinstance(obj, unicode):
461 return obj.encode('utf-8')
462 else:
463 return obj
358def ConvertDictToText(d):
359 yield '%s' % json.dumps(d)
25def python_to_json(python_data):
26 '''Convert python data (tuple, list, dict, etc) into json string'''
27 json_str = json.dumps(python_data, indent=4)
28 return json_str
77def strToDict(string):
78 try:
79 return ast.literal_eval(string)
80 except ValueError as e:
81 logger.log(CUSTOM_LOGGING.ERROR, "conv string failed : %s" % e)

Related snippets