如何在Python 3中忽略行而编写json文件?

时间:2019-05-06 15:30:58

标签: python json api

我正在使用API​​:https://app.omie.com.br/developer

问题是我需要将请求放入循环中,以使用某些方法获取所有页面。当我转储文件时,我遇到一个问题,即:json文件打开和关闭每个循环,因此我无法将它们全部放入一个文件。

有什么方法可以忽略每个json响应的第一行和最后一行吗?

import requests
import json

response = str(requests.get('https://app.omie.com.br/api/v1/financas/contapagar/?JSON={"call":"ListarContasPagar","app_key":"1560731700","app_secret":"226dcf372489bb45ceede61bfd98f0f1","param":[{"pagina":1,"registros_por_pagina":20,"apenas_importado_api":"N"}]}'))
if response != "<Response [200]>":
    print("Invalid Data")
else:
    response = requests.get('https://app.omie.com.br/api/v1/financas/contapagar/?JSON={"call":"ListarContasPagar","app_key":"1560731700","app_secret":"226dcf372489bb45ceede61bfd98f0f1","param":[{"pagina":1,"registros_por_pagina":20,"apenas_importado_api":"N"}]}').json()
    for i in range(1, response["total_de_paginas"] + 1):
        response = requests.get('https://app.omie.com.br/api/v1/financas/contapagar/?JSON={"call":"ListarContasPagar","app_key":"1560731700","app_secret":"226dcf372489bb45ceede61bfd98f0f1","param":[{"pagina":%s,"registros_por_pagina":20,"apenas_importado_api":"N"}]}'%i).json()
        if i == 1:
            mode = "w"
        else:
            mode = "a"
        with open("resultado.json", mode) as rs:
            json.dump(response,rs, indent=4)
        with open("resultado.json", "a") as rs: #I added this line expecting that would help fixing my problem, but it didn't
            rs.write(",\n")
        print("Page completed: %s" %i)

Json File的第一行和最后一行如下:

第一

{
    "registros": 20,
    "conta_pagar_cadastro": [

最后

    ],
    "total_de_paginas": 11,
    "total_de_registros": 215,
    "pagina": 1
},
{

最后一个“ {”是下一页json响应的开始。

运行代码时,我需要忽略这一行。 我该怎么办?

0 个答案:

没有答案