Aug-17-2017, 02:58 PM
I would like to navigate through a complex JSON Dictionary and format it to be usable in an Excel file. I am fairly new to Python I've only been working with it for a couple of days.
type(data['features'])
Out[71]: list
type(data['features'][0]['attributes'])
Out[72]: dict
Additionally, I don't want to have to define a range for the index i = 0 to i = 100, coming from Excel VBA I am looking for something like usedrange.rows.count which essentially returns a count of the number of rows with data in them. That way my ranges could be dynamic if I use the same code with a different URL.
The current code works but the output is crappy and I could do some excel manipulation to clean it up but why not shoot for a solution where python does it for me?
I really
the way pprint() makes it look.
OUCH I CANT POST CLICKABLE LINKS
data_url = 'x'
x has been deleted so I can post the link
import requests import json from pprint import pprint import openpyxl from openpyxl import Workbook wb = Workbook() ws = wb.active dest_filename = 'MyTestSheet.xlsx' ws.title = "MyTestSheet" data_url = '' response = requests.get(data_url) response.raise_for_status() data = response.json() x = 1 y = 1 for i in range(0,100): ws.cell(row=x, column=y, value=str(data['features'][i]['attributes'])) x = x+1 wb.save('MyTestSheet.xlsx')What I want to do is print each item in the 'attributes' dictionary to its own row and then move to the next column and do the same for the next 'attributes' in 'features'. I know I can access the individual 'attributes' using the index like below.
data['features'][0]['attributes']What I don't know how to do is how to loop through each string in the 'attributes' dictionary and print it to its own row in excel.
data['features'][0]['attributes'][0]Above doesn't work and I think I know why its because:
type(data['features'])
Out[71]: list
type(data['features'][0]['attributes'])
Out[72]: dict
Additionally, I don't want to have to define a range for the index i = 0 to i = 100, coming from Excel VBA I am looking for something like usedrange.rows.count which essentially returns a count of the number of rows with data in them. That way my ranges could be dynamic if I use the same code with a different URL.



The current code works but the output is crappy and I could do some excel manipulation to clean it up but why not shoot for a solution where python does it for me?
I really

OUCH I CANT POST CLICKABLE LINKS

data_url = 'x'
x has been deleted so I can post the link