我有这个字符串,我想把它转换成一个列表:
"{'Attributes': {'a', 'b', 'h'}, 'Group3': {'c'}, 'Group2': {'s', 'm', 'r', 'ac'}}"
我尝试过json.loads()
,它给出了这个错误:
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
它来自另一个程序,我无法控制引用...单引号或双引号
环境: Python 3.x with import json
我的代码:
mystr = "{'Attributes': {'a', 'b', 'h'}, 'Group3': {'c'}, 'Group2': {'s', 'm', 'r', 'ac'}}"
mylist = json.loads(mystr)
我希望它是一个有效的列表
发布于 2019-05-29 06:42:12
您的字符串不是有效的JSON。
下面的代码应该可以解决这个问题:
import json
mystr = "{'Attributes': ['a', 'b', 'h'], 'Group3': ['c'], 'Group2': ['s', 'm', 'r', 'ac']}"
mydict = json.loads(mystr.replace("'", "\""))
print(mydict)
>>> {'Attributes': ['a', 'b', 'h'], 'Group3': ['c'], 'Group2': ['s', 'm', 'r', 'ac']}
https://stackoverflow.com/questions/56350872
复制相似问题