从string中生成dict

20230220

using json.loads().

import json
original_string = '{"John" : "01", "Rick" : "02", "Sam" : "03"}' 
  
# printing original string 
print("The original string is : " + str(original_string))
  
# using json.loads() method
result = json.loads(original_string)
  
# print result
print("The converted dictionary is  : " + str(result))

最好是""双引号。json默认双引号内的是dict。

参考:
[1] https://favtutor.com/blogs/string-to-dict-python
[2] https://stackoverflow.com/questions/39491420/python-jsonexpecting-property-name-enclosed-in-double-quotes
[3] https://www.geeksforgeeks.org/python-program-to-create-a-dictionary-from-a-string/
[4] https://favtutor.com/blogs/string-to-dict-python

posted @ 2023-02-20 17:51  xiaoxuxli  阅读(76)  评论(0编辑  收藏  举报