Python 无法使用json.loads将字符串加载到对象中
我正在尝试将字符串加载到python对象中。我得到以下错误: 这是错误:期望值:第15行第15列(字符351) {'allData':无} 当我将cprob的值更改为以下代码时,没有问题:Python 无法使用json.loads将字符串加载到对象中,python,json,oop,Python,Json,Oop,我正在尝试将字符串加载到python对象中。我得到以下错误: 这是错误:期望值:第15行第15列(字符351) {'allData':无} 当我将cprob的值更改为以下代码时,没有问题: "cprob": { "T": 1, "A": 2, "C": 3 } test.txt中的数据: [{ "V": ["Offer", "Interview", "Grades", "Admission", "Exper
"cprob": {
"T": 1,
"A": 2,
"C": 3
}
test.txt中的数据:
[{
"V": ["Offer", "Interview", "Grades", "Admission", "Experience"],
"E": [["Grades", "Interview"],
["Experience", "Interview"],
["Grades", "Admission"],
["Interview", "Offer"]],
"Vdata": {
"Offer": {
"ord": 4,
"numoutcomes": 2,
"vals": ["0", "1"],
"parents": ["Interview"],
"children": 0,
"cprob": {
"['0']": [.9, .1],
"['1']": [.4, .6],
"['2']": [.01, .99]
}
}
}
}]
代码:
import json
class JsonData:
def __init__(self, path):
self.allData = None
def dictload(self, path):
f = open(path, 'r')
ftext = f.read()
print(ftext)
try:
self.allData = json.loads(ftext)
except Exception as e:
print('This is error : ', e)
path = "test.txt"
jsonData = JsonData(path)
jsonData.dictload(path)
print(vars(jsonData))
python的json模块似乎不理解
.1
是0.1
。如果您将0
添加到句点前面,它将起作用。非常感谢。多么简单的解决方案啊。太好了,它很管用。论坛说,我可以在8分钟内接受答案:)@NagaVemprala或者使用ast
模块<代码>打开('test.txt')作为f:d=ast.literal\u eval(f.read())@Chris\u Rands ast.literal\u eval也能很好地工作。非常感谢。我想我不会选择json.loadagain@Chris_RandsJavascript的JSON.parse也无法解析它。但是jq
可以。“数字是一个十进制数字序列,没有多余的前导零。”但图中显示的是句点前的0
,因此这似乎是必要的。