gpt4 book ai didi

JSON解码错误: Expecting value: line 1 column 1

转载 作者:行者123 更新时间:2023-12-03 01:24:08 25 4
gpt4 key购买 nike

我在 Python 3.5.1 中收到此错误。

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这是我的代码:

import json
import urllib.request

connection = urllib.request.urlopen('http://python-data.dr-chuck.net/comments_220996.json')

js = connection.read()

print(js)

info = json.loads(str(js))

image

最佳答案

如果您查看从 print() 以及回溯中收到的输出,您将看到返回的值不是字符串,而是一个字节对象(前缀为 b):

b'{\n  "note":"This file    .....

如果您使用 curl -v 等工具获取 URL,您将看到内容类型为

Content-Type: application/json; charset=utf-8

所以它是 JSON,编码为 UTF-8,Python 将其视为字节流,而不是简单的字符串。为了解析它,您需要先将其转换为字符串。

将最后一行代码更改为:

info = json.loads(js.decode("utf-8"))

关于JSON解码错误: Expecting value: line 1 column 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34579327/

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