我正在嘗試將字符串加載到 python 對象中。我收到以下錯誤:這是錯誤:期望值:第 15 行第 15 列(字符 351){'allData':無}當我將 cprob 的值更改為以下代碼時,沒有問題: "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 jsonclass 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))
添加回答
舉報
0/150
提交
取消