猿问

无法使用 json.loads 将字符串加载到对象中

我正在尝试将字符串加载到 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 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))


慕少森
浏览 227回答 1
1回答

波斯汪

似乎 python 的 json 模块不理解.1为0.1. 如果您0在句点前添加,它将起作用。
随时随地看视频慕课网APP

相关分类

Python
我要回答