【发布时间】:2021-07-21 04:01:48
【问题描述】:
我有以下代码sn-p:
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) 文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/init.py”, 第 354 行,在负载中 返回 _default_decoder.decode(s) 文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py”, 第 339 行,在解码中 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py” , 第 355 行,在 raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 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"
}
【问题讨论】:
-
您的预期输出是什么?你能编辑你的问题并把它放在那里吗?
-
不要使用
input作为变量,它是python中的一个函数,这会导致一些错误。 -
@AndrejKesely 我已经用输出值更新了问题。