奇怪的python问题,"unicode"对象没有属性"read" [英] strange python issue, 'unicode' object has no attribute 'read'

查看:347
本文介绍了奇怪的python问题,"unicode"对象没有属性"read"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,是否有人知道哪里出了问题?我打开我的JSON内容直接通过浏览器运行,并且有效

Here is my code and does anyone have any ideas what is wrong? I open my JSON content directly by browser and it works,

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

先谢谢了, 林

推荐答案

由于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()

这篇关于奇怪的python问题,"unicode"对象没有属性"read"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆