Python, преобразующий JSON в операторы SQL Insert - PullRequest
0 голосов
/ 05 ноября 2019

Я создал фрагмент Python, который выводит файл JSON, который подбирается и вставляется в наши таблицы, но я подумал, что могу ускорить процесс, просто не создавая файл JSON и создав вместо него файл операторов SQL INSERT.

Это действующая версия, которая выбирает файл:

import os
import json
Basenames = 'C:\\Users\\Desktop\\JSON\\05174546.json'

with open(Basenames,'x') as outfile: #Creates the Json file
            json.dump(x.to_table(), outfile)

with open (Basenames,'r') as f: #Opens the Json file
jsondata = json.loads(f.read())
f.close()

for json in jsondata: #Loops through the Json file
    keylist = "("
    valuelist = "("
    firstPair = True
    for key, value in json.items():
        if not firstPair:
            keylist += ", "
            valuelist += ", "
        firstPair = False
        keylist += key
        if type(value) in (str, "utf-8"):
            valuelist += "'" + value + "'"
        else:
            valuelist += str(value)
    keylist += ")"
    valuelist += ")"

    sqlstatement += "INSERT INTO " + TABLE_NAME + " " + keylist + " VALUES " + valuelist + "\n"

print(sqlstatement)

Что бы я хотел сделать вместо этого, я просто хочу установить json.dump (x.to_table ()) равнымоднако массив, через который может пройти следующий бит, когда я попробовал это, я получаю ошибку:

AttributeError: у объекта 'str' нет атрибута 'items'

[
    {
        "name": "PropertyPlantEquipment",
        "value": 3730.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "PropertyPlantEquipment",
        "value": 4119.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "Debtors",
        "value": 1256.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "Debtors",
        "value": 12132.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "CashBankOnHand",
        "value": 3397.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "CashBankOnHand",
        "value": 48536.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "CurrentAssets",
        "value": 4653.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "CurrentAssets",
        "value": 60668.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "Creditors",
        "value": 18392.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments"
    },
    {
        "name": "Creditors",
        "value": 32205.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments"
    },
    {
        "name": "NetCurrentAssetsLiabilities",
        "value": -13739.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "NetCurrentAssetsLiabilities",
        "value": 28463.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "TotalAssetsLessCurrentLiabilities",
        "value": 10009.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "TotalAssetsLessCurrentLiabilities",
        "value": -32582.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "Creditors",
        "value": 0.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:Non-currentFinancialInstruments"
    },
    {
        "name": "Creditors",
        "value": 0.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:Non-currentFinancialInstruments"
    },
    {
        "name": "NetAssetsLiabilities",
        "value": -10009.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "NetAssetsLiabilities",
        "value": 32582.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "Equity",
        "value": 2.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:EquityClassesDimension core:ShareCapital"
    },
    {
        "name": "Equity",
        "value": 2.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:EquityClassesDimension core:ShareCapital"
    },
    {
        "name": "Equity",
        "value": -10011.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:EquityClassesDimension core:RetainedEarningsAccumulatedLosses"
    },
    {
        "name": "Equity",
        "value": 32580.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:EquityClassesDimension core:RetainedEarningsAccumulatedLosses"
    },
    {
        "name": "Equity",
        "value": -10009.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "Equity",
        "value": 32582.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": ""
    },
    {
        "name": "AverageNumberEmployeesDuringPeriod",
        "value": 1.0,
        "unit": "xbrli:pure",
        "instant": null,
        "startdate": "2018-02-01",
        "enddate": "2019-01-31",
        "segment:0": ""
    },
    {
        "name": "TradeDebtorsTradeReceivables",
        "value": 0.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments",
        "segment:1": "explicitMember core:MaturitiesOrExpirationPeriodsDimension core:WithinOneYear"
    },
    {
        "name": "TradeDebtorsTradeReceivables",
        "value": 0.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments",
        "segment:1": "explicitMember core:MaturitiesOrExpirationPeriodsDimension core:WithinOneYear"
    },
    {
        "name": "OtherDebtors",
        "value": 1256.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments",
        "segment:1": "explicitMember core:MaturitiesOrExpirationPeriodsDimension core:WithinOneYear"
    },
    {
        "name": "OtherDebtors",
        "value": 12132.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments",
        "segment:1": "explicitMember core:MaturitiesOrExpirationPeriodsDimension core:WithinOneYear"
    },
    {
        "name": "Debtors",
        "value": -1256.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments",
        "segment:1": "explicitMember core:MaturitiesOrExpirationPeriodsDimension core:WithinOneYear"
    },
    {
        "name": "Debtors",
        "value": -12132.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments",
        "segment:1": "explicitMember core:MaturitiesOrExpirationPeriodsDimension core:WithinOneYear"
    },
    {
        "name": "TradeCreditorsTradePayables",
        "value": 0.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments"
    },
    {
        "name": "TradeCreditorsTradePayables",
        "value": 0.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments"
    },
    {
        "name": "OtherTaxationSocialSecurityPayable",
        "value": 18392.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments"
    },
    {
        "name": "OtherTaxationSocialSecurityPayable",
        "value": 21266.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments"
    },
    {
        "name": "OtherCreditors",
        "value": 0.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments"
    },
    {
        "name": "OtherCreditors",
        "value": 10939.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments"
    },
    {
        "name": "Creditors",
        "value": 18392.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments"
    },
    {
        "name": "Creditors",
        "value": 32205.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:CurrentFinancialInstruments"
    },
    {
        "name": "TradeCreditorsTradePayables",
        "value": 0.0,
        "unit": "iso4217:GBP",
        "instant": "2019-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:Non-currentFinancialInstruments"
    },
    {
        "name": "TradeCreditorsTradePayables",
        "value": 0.0,
        "unit": "iso4217:GBP",
        "instant": "2018-01-31",
        "startdate": null,
        "enddate": null,
        "segment:0": "explicitMember core:FinancialInstrumentCurrentNon-currentDimension core:Non-currentFinancialInstruments"
    },
    {
        "name": "ProfitLoss",
        "value": 53110.0,
        "unit": "iso4217:GBP",
        "instant": null,
        "startdate": "2018-02-01",
        "enddate": "2019-01-31",
        "segment:0": ""
    },
    {
        "name": "DividendsPaid",
        "value": -95701.0,
        "unit": "iso4217:GBP",
        "instant": null,
        "startdate": "2018-02-01",
        "enddate": "2019-01-31",
        "segment:0": "explicitMember core:EquityClassesDimension core:RetainedEarningsAccumulatedLosses"
    }
]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...