我怎么能保存我刮的json数据集,这是一个文本格式到我的本地机器,也我怎么能读取文件到熊猫DataFrame?<



代码如下:
我想用它来分析

response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
{"@type":"imdb.api.title.ratings","id":"/title/tt0944947/","title":"Game of Thrones","titleType":"tvSeries","year":2011,"canRate":true,"otherRanks":[{"id":"/chart/ratings/toptv","label":"Top 250 TV","rank":12,"rankType":"topTv"}],"rating":9.2,"ratingCount":1885115,"ratingsHistograms":{"Males Aged 18-29":{"aggregateRating":9.3,"demographic":"Males Aged 18-29","histogram":{"1":11186,"2":693,"3":801,"4":962,"5":2103,"6":3583,"7":9377,"8":22859,"9":52030,"10":174464},"totalRatings":278058},"IMDb Staff":{"aggregateRating":8.7,"demographic":"IMDb Staff","histogram":{"1":0,"2":0,"3":0,"4":0,"5":1,"6":3,"7":6,"8":19,"9":27,"10":17},"totalRatings":73}

坦率地说,您应该在任何Python教程或requests的许多示例中找到它

fh = open("output.json")
fh.write(response.text)
fh.close()

with open("output.json") as fh:
fh.write(response.text)

至于熊猫,你可以试着读一下

df = pd.read_json("output.json")

或者您可以使用模块io来读取它,而不需要在磁盘上保存

import io
fh = io.StringIO(response.text)
df = pd.read_json(fh)

但是pandas保持数据作为表的行和列,但是你有嵌套的列表/字典,所以它可能需要一些工作来保持它在DataFrame

如果你只想从json中获取一些数据,那么你可以使用response.json()

相关内容

  • 没有找到相关文章

最新更新