10 examples of 'convert string to dict python' in Python

Every line of 'convert string to dict 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
this disclaimer
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
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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)
53def _string_to_dict(s):
54 return {name: value for (name, value) in (v.split('=') for v in s.split(' ') if v)}
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
358def ConvertDictToText(d):
359 yield '%s' % json.dumps(d)
5def dict_conversion(input):
6 try:
7 return literal_eval(input)
8 except Exception:
9 return loads(input)
255def _to_dict(contents):
256 """Parse |contents| as a dict, returning None on failure or if it's not a
257 dict."""
258 try:
259 result = ast.literal_eval(contents)
260 if isinstance(result, dict):
261 return result
262
263 except (ValueError, TypeError):
264 pass
265
266 return None
15def _str_to_data(string):
16 try:
17 return ast.literal_eval(str(string).strip())
18 except Exception:
19 return string
352def json2dict(s):
353 if isinstance(s, dict):
354 return s
355 s = str(s)
356 d = {}
357 try:
358 d = json.loads(s)
359 except:
360 try:
361 d = eval(s)
362 except:
363 s = s.replace('true', 'True').replace('false', 'False').replace(
364 'null', 'None').replace('none', 'None')
365 d = eval(s)
366 return d
102def mkdict(s):
103 xdict = {}
104 if isinstance(s, str):
105 s = s.split()
106 for kvstr in s:
107 k,v = kvstr.split('=')
108 xdict[k] = v
109 return xdict

Related snippets