gpt4 book ai didi

python - 值错误: Expecting property name enclosed in double quotes in Python

转载 作者:太空宇宙 更新时间:2023-11-03 16:39:55 24 4
gpt4 key购买 nike

旧错误:我遇到了这个错误:

ValueError: Expecting property name enclosed in double quotes:

这是我的代码。我想将 String ResultPart 转换为字典:

 resultPart = '{"sentences": [{"parsetree": [], [("words": "Q", {"Lemma": "q", "NamedEntityTag": "O", "CharacterOffsetEnd": "1", "PartOfSpeech": "NN", "CharacterOffsetBegin": "0"})], "dependencies": [], "text": "Q", "parsetree": [], "indexeddependencies": []}]}'
resultPart2 = json.dumps(resultPart)
#result should be a dict
result = json.loads(resultPart)

编辑:我更正了该部分,现在出现此错误:

TypeError: string indices must be integers

这是新代码:

 resultPart = "{'sentences': [{'words': [('Q', {'Lemma': 'q', 'NamedEntityTag': 'O', 'CharacterOffsetEnd': '1', 'PartOfSpeech': 'NN', 'CharacterOffsetBegin': '0'})], 'dependencies': [], 'text': 'Q', 'parsetree': [], 'indexeddependencies': []}]}"
resultPart2 = json.dumps(resultPart)
result = json.loads(resultPart2)

最佳答案

问题似乎出在以下行:

{
...
"parsetree": [], [
"words": "Q", {
"Lemma": "q",
"NamedEntityTag": "O",
"CharacterOffsetEnd": "1",
"PartOfSpeech": "NN",
"CharacterOffsetBegin": "0"
}],
...

}

它正在寻找[]之后的另一个作为“parsetree”的值。因此,它需要类似于以下内容才能成为有效的 JSON。

{
...
"parsetree": [],
"more_words": [
"words": "Q", {
"Lemma": "q",
"NamedEntityTag": "O",
"CharacterOffsetEnd": "1",
"PartOfSpeech": "NN",
"CharacterOffsetBegin": "0"
}],
...

}

关于python - 值错误: Expecting property name enclosed in double quotes in Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36901079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com