简体   繁体   中英

Looping thru .JSON dictionary and inserting items via SQLite3

I am trying to iterate through a .json list. I want to create separate entries in SQLite3 for each of these three sub-lists in 'data'.

data = {u'symbols':[{u'optionType': None, u'dividendDate': u'2017-12-29T00:00:00.000000-05:00', u'eps': 2.79, u'currency': u'CAD', u'prevDayClosePrice': 32.41, u'averageVol3Months': 579272, u'industrySubgroup': u'InsuranceLife', u'isTradable': True, u'hasOptions': True, u'optionDurationType': None, u'optionExpiryDate': None, u'symbolId': 32304, u'outstandingShares': 415289000, u'tradeUnit': 1, u'optionStrikePrice': None, u'pe': 11.62, u'lowPrice52': 28.4, u'industrySector': u'FinancialServices', u'optionRoot': u'', u'description': u'POWER CORP OF CDA SV', u'symbol': u'POW.TO', u'securityType': u'Stock', u'dividend': 0.3585, u'minTicks': [{u'pivot': 0, u'minTick': 0.005}, {u'pivot': 0.5, u'minTick': 0.01}], u'averageVol20Days': 506545, u'industryGroup': u'InsuranceLife', u'optionContractDeliverables': {u'cashInLieu': 0, u'underlyings': []}, u'highPrice52': 33.69, u'yield': 4.43, u'listingExchange': u'TSX', u'isQuotable': True, u'optionExerciseType': None, u'marketCap': 13459516489, u'exDate': u'2017-12-07T00:00:00.000000-05:00'}, 

{u'optionType': None, u'dividendDate': u'2018-02-01T00:00:00.000000-05:00', u'eps': 2.95, u'currency': u'CAD', u'prevDayClosePrice': 35.28, u'averageVol3Months': 394431, u'industrySubgroup': u'InsuranceLife', u'isTradable': True, u'hasOptions': True, u'optionDurationType': None, u'optionExpiryDate': None, u'symbolId': 32316, u'outstandingShares': 713871000, u'tradeUnit': 1, u'optionStrikePrice': None, u'pe': 11.96, u'lowPrice52': 31.75, u'industrySector': u'FinancialServices', u'optionRoot': u'', u'description': u'POWER FINANCIAL CP', u'symbol': u'PWF.TO', u'securityType': u'Stock', u'dividend': 0.4125, u'minTicks': [{u'pivot': 0, u'minTick': 0.005}, {u'pivot': 0.5, u'minTick': 0.01}], u'averageVol20Days': 396594, u'industryGroup': u'InsuranceLife', u'optionContractDeliverables': {u'cashInLieu': 0, u'underlyings': []}, u'highPrice52': 37, u'yield': 4.68, u'listingExchange': u'TSX', u'isQuotable': True, u'optionExerciseType': None, u'marketCap': 25185368880, u'exDate': u'2017-12-28T00:00:00.000000-05:00'}, 

{u'optionType': None, u'dividendDate': u'2017-12-07T00:00:00.000000-05:00', u'eps': 2.03, u'currency': u'CAD', u'prevDayClosePrice': 24.85, u'averageVol3Months': 589611, u'industrySubgroup': u'REITRetail', u'isTradable': True, u'hasOptions': True, u'optionDurationType': None, u'optionExpiryDate': None, u'symbolId': 34330, u'outstandingShares': 326529000, u'tradeUnit': 1, u'optionStrikePrice': None, u'pe': 12.25, u'lowPrice52': 23.46, u'industrySector': u'RealEstate', u'optionRoot': u'', u'description': u'RIOCAN REAL EST INV TR', u'symbol': u'REI.UN.TO', u'securityType': u'Stock', u'dividend': 0.1175, u'minTicks': [{u'pivot': 0, u'minTick': 0.005}, {u'pivot': 0.5, u'minTick': 0.01}], u'averageVol20Days': 575707, u'industryGroup': u'REITs', u'optionContractDeliverables': {u'cashInLieu': 0, u'underlyings': []}, u'highPrice52': 27.25, u'yield': 5.68, u'listingExchange': u'TSX', u'isQuotable': True, u'optionExerciseType': None, u'marketCap': 8114245650, u'exDate': u'2017-11-29T00:00:00.000000-05:00'}]}

This is part of the code I have (I think the problem is due a misunderstanding on how the for loop interacts with the son data):

indice = -1    

for y in data:

            indice +=1

            data = data['symbols'][indice]
            try:
                conn = sqlite3.connect('sql/investment_databse.db')
                c = conn.cursor()
                with conn:
                    c.execute('''INSERT INTO symbols(symbol,...

The code works to insert only the first item in data. I want it to loop through all three.

I thought that if I increase the [indice] by 1 every loop it would cycle through. It seems to me that Python thinks there is only one element in the data to cycle through.

Can you help me understand why my code skips over the second two items and help write a code that will insert all three elements of the data dictionary as separate SQL entries?

Thanks!

There is only one element in data ; the key symbols .

It is data["symbols"] you should be iterating over. And you don't need indice at all.

for symbol in data["symbols"]:
   print(symbol["optionType"]) # etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM