我有以下代码段:
input = "You can check it out here. https://www.youtube.com/watch?v=Ay1gCPAUnxo&t=83s I'll send $20 in bitclout to the first 50 people that follow instructions at end of the video. This is revolutionary. Let's hope it works! <3Building it. What's up y'all"
def createJsonText(input):
input = r'{}'.format(input)
x = r'{ "text":"' + input + r'"}'
print(x)
# parse x as json
y = json.loads(x)
f = open("tone.json", "a")
f.write(str(y))
f.close()
执行上述代码时,出现以下错误:
文件“hashtag analyzer.py”,第x行,在readjson createjsontext(input)文件“hashtag analyzer.py”中,第y行,在createjsontext y=json.loads(x)file“/library/frameworks/python.framework/versions/3.6/lib/python3.6/json/init.py”中,第354行,在loads return“default\u decoder.decode(s)file”/library/frameworks/python.framework/versions/3.6/lib/python3.6/json/decoder.py”中,第339行,在decode obj中,end=self.raw\u decode(s,idx=\u w(s,0.end())file“/library/frameworks/python.framework/versions/3.6/lib/python3.6/json/decoder.py”中,第355行,在raw\u decode obj中,end=self.scan\u,idx)json.decoder.jsondecodeerror:应为','分隔符:第1行第4194列(char 4193)
如何解决?
预期输出是名为“tone.json”的json文件,其中包含以下数据:
{
"text": "You can check it out here. https://www.youtube.com/watch?v=Ay1gCPAUnxo&t=83s I'll send $20 in bitclout to the first 50 people that follow instructions at end of the video. This is revolutionary. Let's hope it works! <3Building it. What's up y'all"
}
1条答案
按热度按时间kuarbcqp1#
如果您想创建json,那么这里的方向就错了。你想要什么
dumps
,不是loads
':您的代码具有append的mode='a',但一组单独的json行不是有效的json文件。如果希望它是一个有效的json文件,那么需要将整个文件作为一个文档。
更新
或者: