且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

奇怪的python问题,"unicode"对象没有属性"read"

更新时间:2023-09-11 22:28:16

由于data是unicode/str变量而引发此错误,请更改代码的第二行以解决错误:

This error raised because the data is a unicode/str variable, change the second line of your code to resolve your error:

data = json.loads(data)

json.load在第一个参数位置获取一个文件对象,并调用此对象的read方法.

json.load get a file object in first parameter position and call the read method of this.

您还可以调用响应的json方法直接获取数据:

Also you can call the json method of the response to fetch data directly:

response = requests.get('http://ws.audioscrobbler.com/2.0/?method=library.getartists&api_key=4c22bd45cf5aa6e408e02b3fc1bff690&user=joanofarctan&format=json')
data = response.json()