python解析json数据出错,无法使用python读取json文件。获取类型错误:json对象为“ TextIOWrapper”...

我正在尝试从json文件读取。

这是我创建文件的方式:

import requests

import json

import time

from pprint import pprint

BASE_URL = "https://www.wikiart.org/en/api/2/UpdatedArtists"

artist_json_data = requests.get(BASE_URL).json()

with open('artistdata.json', 'w') as outfile:

while artist_json_data['hasMore']:

print(artist_json_data['paginationToken'])

url = BASE_URL + "?paginationToken=" +artist_json_data['paginationToken']

artist_json_data = requests.get(url).json()

json.dump(artist_json_data, outfile, indent=4)

time.sleep(1)

这是我输出的开始:

{

"data": [

{

"id": "57726da5edc2cb3880b4ca54",

"artistName": "Paul Feeley",

"url": "paul-feeley",

"lastNameFirst": "Feeley Paul",

"birthDay": "/Date(-1893456000000)/",

"deathDay": "/Date(-126230400000)/",

当我尝试使用以下代码读取同一文件时:

from pprint import pprint

with open('artistdata.json', 'r', encoding='utf-8') as data_file:

data = json.loads(data_file)

pprint(data)

我得到错误

TypeError: the JSON object must be str, bytes or bytearray, not 'TextIOWrapper'

我不明白,因为我可以像往常一样崇高地打开文件。我该如何处理?

使用以下代码解决了该问题:

问题是我混合了转储和负载。现在我正在使用转储和加载

class Wikiart:

'''Class to access wikiart.org Data'''

def __init__(self):

self.BASE_URL = "https://www.wikiart.org/en/"

self.BASE_URL_API = self.BASE_URL + "api/2/"

self.BASE_URL_MOVEMENT = self.BASE_URL + 'artists-by-art-movement/'

self.ARTIST_DATA_URL = self.BASE_URL_API + "UpdatedArtists"

def write_artist_data_into_json_file(self):

artists = requests.get(ARTIST_DATA_URL).json()

all_artists = artists['data']

with open('artistdata.json', 'w') as outfile:

while artists['hasMore']:

print('fetching next: pagination token',artists['paginationToken'])

url = BASE_URL + "?paginationToken=" + artists['paginationToken']

artists_next_page = requests.get(url).json()

next_artists = artists_next_page['data']

time.sleep(0.25)

all_artists = all_artists + next_artists

artists = artists_next_page

json.dump(all_artists, outfile, indent=4)

from pprint import pprint

with open('artistdata.json', 'r', encoding='utf-8') as data_file:

data = json.load(data_file)

pprint(data)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值