【问题标题】:can't read json file with python. getting type error: json object is 'TextIOWrapper'无法用 python 读取 json 文件。获取类型错误:json 对象是 'TextIOWrapper'
【发布时间】:2017-10-17 13:29:43
【问题描述】:

我正在尝试从 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)

【问题讨论】:

    标签: json python-3.x


    【解决方案1】:

    3种加载json文件的方法:

    import json
    import ast
    with open(file_path) as file:
        data1 = json.load(file)
        data2 = json.loads(file.read())  
        data3 = ast.literal_eval(file.read())
    

    should use json.load whenever possible,但有时 JSON 文件的格式不正确(例如,单引号而不是双引号)。一个解决方案是使用 ast.literal_eval()

    【讨论】:

      【解决方案2】:

      使用json.load()(不带's')代替json.loads()

      PS 从文件加载时您将使用json.load()。和json.loads() 使用字符串时:)

      【讨论】:

        【解决方案3】:

        json.load() 用于加载文件。 json.loads() 适用于字符串。

        【讨论】:

        • json.load() 也给我一个错误JSONDecodeError: Extra data: line 1960 column 2 (char 94439)
        • 显然我把事情搞混了。让我深入研究一下
        猜你喜欢
        • 2016-11-28
        • 2022-11-27
        • 2012-10-11
        • 2021-08-07
        • 1970-01-01
        • 1970-01-01
        • 2018-09-01
        • 1970-01-01
        相关资源
        最近更新 更多