8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

在分页 API 时附加 JSON 文件会创建新的方括号

Paul Collingwood 1月前

12 0

我是 Python 新手,在通过 API 分页时,我很难让 JSON 正确地附加新的对象数组。API 上的每个新页面都用一对方括号括起来,

我是 Python 新手,当我通过 API 进行分页时,我正在努力让我的 JSON 正确附加新的对象数组。

API 上的每个新页面都用一对方括号括起来,当我附加 JSON 文件时,它们会重新出现,从而导致其无法使用。

import requests
import json
import os

# enter get() information

url = "http://311api.cityofchicago.org/open311/v2/requests.json"
pageload = {'page_size': '500', 'page': 1}

headers = {'Accept': 'application/vnd.github.v3+json'}

rats = []

exist = False

# create loop that goes until a specified page in the API

while pageload['page'] < 3:
    response = requests.get(url, headers=headers, params=pageload)
    response_dict = response.json()

# isolate loop for rats
    for r in response_dict:
        if r['service_name'] == 'Rodent Baiting/Rat Complaint':
            rat = r
            rats.append(rat)
        
#paginate by 1
    pageload['page'] += 1

#dump data into file if file is blank
readable_file = 'readable-all-rats_2.json'
with open(readable_file, 'a') as file:
    # If the file is empty
    if os.path.getsize(readable_file) == 0:
        json.dump(rats, file, indent=4)
    else:
        print("Existing file found!")
        #create trigger to cue next segment
        exist = True

if exist:
    # create object to save new complaints in
    new_complaints = []
    # dump file into formatted json
    with open(readable_file, 'r') as f:
        existing_file = json.load(f)
    for service_request_a, service_request_b in zip(existing_file,rats):
        if service_request_a['service_request_id'] != service_request_b['service_request_id']:
            #print new service request numbers
            print(f"{service_request_a['service_request_id']} != {service_request_b['service_request_id']}: New case!")
            new_complaint = service_request_b
            new_complaints.append(new_complaint)
        else:
            print("Nothing but old cases.")

#write new complaints to file

#identify that new_complaints has data in it
if exist:
    if len(new_complaints) != 0:
        with open(readable_file, 'a') as f:
            f.seek(0)
            json.dump(new_complaints, f, indent=4)
            print("Adding new data to existing complaint!")

运行成功,但创建了一个损坏的 JSON,不允许其随后进行修改。救命!

    },
    {
        "service_request_id": "SR24-01711975",
        "status": "open",
        "service_name": "Rodent Baiting/Rat Complaint",
        "service_code": "4fd3b9bce750846c5300004a",
        "requested_datetime": "2024-09-10T20:13:18Z",
        "updated_datetime": "2024-09-10T20:13:21Z",
        "address": "3914 N Ottawa Ave",
        "lat": 41.951223000940686,
        "long": -87.81874200000193,
        "token": "66e0a87858a825a955055d69"
    }
][
    {
        "service_request_id": "SR24-01694217",
        "status": "open",
        "service_name": "Rodent Baiting/Rat Complaint",
        "service_code": "4fd3b9bce750846c5300004a",
        "requested_datetime": "2024-09-07T21:21:31Z",
        "updated_datetime": "2024-09-12T21:21:59Z",
        "address": "3053 W Cortland St",
        "lat": 41.9155240040132,
        "long": -87.70447922778453,
        "token": "66dcc40358a825a9550409c6"
帖子版权声明 1、本帖标题:在分页 API 时附加 JSON 文件会创建新的方括号
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Paul Collingwood在本站《python》版块原创发布, 转载请注明出处!
最新回复 (0)
返回
作者最近主题: