Parse json with python and access to inner nodes - python

Hello I want to parse this json file in python like sql database column as below:
row #1 order id: 317246190-A can_refund: false cancelations_amount:15.13 offer_sku:3803372 order_line_id: 317246190-A-1
row #2 order id: 317246190-A can_refund: false cancelations_amount:15.13 offer_sku:3803372 order_line_id: 317246190-A-2
here is the json file :
{
"orders": [
{
"order_id": "317246190-A",
"order_lines": [
{
"can_refund": false,
"cancelations": [
{
"amount": 15.13,
"commission_amount": 3.32
}
],
"offer_sku": "3803372",
"order_line_id": "317246190-A-1",
"price": 0.0
},
{
"can_refund": false,
"cancelations": [
{
"amount": 15.13,
"commission_amount": 3.32
}
],
"offer_sku": "3803372",
"order_line_id": "317246190-A-2",
"price": 0.0
}
]
}
],
"total_count": 1
}
I want to parse this json file in python like sql database column as below
row #1 order id: 317246190-A can_refund: false cancelations_amount:15.13 offer_sku:3803372 order_line_id: 317246190-A-1
row #2 order id: 317246190-A can_refund: false cancelations_amount:15.13 offer_sku:3803372 order_line_id: 317246190-A-2

Related

Passing dictionary as parameter to a function

So, I am working on a project in which the user gives inputs in the json file and the parser reads data from the json file and then creates a data structure to which gets updated to the inputs mentioned in the data file.
My json file(input_file.json5) looks like this:
{
"clock_frequency": 25000,
"Triggering_Mode": "positive_edge_triggered",
"Mode": "Offline",
"overshoot": 0.05,
"duty_cycle": 0.5,
"amplitude/high_level": 1,
"offset/low_level": 0
}
The data structure(data_struc.py) looks like this:
Parameters={
"Global_parameters": {
"frequency": 3000,
"Triggering_Mode": "positive_edge_triggered"
},
"Executor_param": {
"Mode": "Offline"
},
"Waveform_Settings": {
"overshoot": 0.05,
"duty_cycle": 0.5,
"amplitude/high_level": 1,
"offset/low_level": 0,
}
}
The code for the parser is:
import json5
from data_struc import Parameters
class Parser(object):
def read_input_file(self, path_name, file_name):
input_file = open(path_name + file_name + '.json5')
data = json5.load(input_file)
print(Parameters['Global_parameters'])
parameters = self.parse_parameters(data)
input_file.close()
return parameters
def parser_parameters(self, data):
parameter = {
"Global_parameters": {
"frequency": data[clock_frequency]
"Triggering_Mode": data[Triggering_Mode]
}
}
return parameter
I want to pass data as a parameter to the function and I want to update the contents of the data structure using the value of the data(passed as dictionary) to the function. How do I implement the function parser_parameters?
Here is a one-liner to map the data to a schema if you can change the schema, you could also just go and grab the keys instead of creating a list of items to match. This formats the data to the schema based on matching keys:
EDIT: added 'Data' tag to the schema and output for nested list data
schema = {
'Global_parameters': [
'clock_frequency', # I noticed you had this as just 'clock' in your desired outuput
'Triggering_Mode'
],
'Executor_param': [
'Mode'
],
'Waveform_Settings': [
'overshoot',
'duty_cycle',
'amplitude/high_level',
'offset/low_level'
],
'Data': {
'Packet'
}
}
data = {
"clock_frequency": 25000,
"Triggering_Mode": "positive_edge_triggered",
"Mode": "Offline",
"overshoot": 0.05,
"duty_cycle": 0.5,
"amplitude/high_level": 1,
"offset/low_level": 0,
"Packet": [
{"time_index":0.1, "data":0x110},
{"time_index":1.21, "data":123},
{"time_index":2.0, "data": 0x45}
]
}
# "one line" nested dict comprehension
data_structured = {k0: {k1: v1 for k1, v1 in data.items() if k1 in v0} # in v0.keys() if you are using the structure you have above
for k0, v0 in schema.items()}
import json
print(json.dumps(data_structured, indent=4)) # pretty print in json format
Output:
{
"Global_parameters": {
"clock_frequency": 25000,
"Triggering_Mode": "positive_edge_triggered"
},
"Executor_param": {
"Mode": "Offline"
},
"Waveform_Settings": {
"overshoot": 0.05,
"duty_cycle": 0.5,
"amplitude/high_level": 1,
"offset/low_level": 0
},
"Data": {
"Packet": [
{
"time_index": 0.1,
"data": 272
},
{
"time_index": 1.21,
"data": 123
},
{
"time_index": 2.0,
"data": 69
}
]
}
}

How to scrape data from a Script element (HTML) to a CSV using Python Selenium

I have a python code that scrapes data from a Script element on a HTML web page. However, the data saved in the csv file (1) is not in the format I want; (2) has extra content not needed. I only want the data for the orange and blue lines and their date. Here is the screenshot of the Canvas on the web page that I need to read data from:
Python code (Partial):
# Locate Edge driver
driver = webdriver.Edge(executable_path="C://Windows//SysWOW64//MicrosoftWebDriver.exe")
# Locate the canvas script element for daily active & peak current players data
Canvas_script = driver.find_element_by_css_selector('body > center > div > div:nth-child(15) > div:nth-child(1) > script')
# Get script content
Canvas_script_data = Canvas_script.get_attribute('innerHTML')
# Create and save data to csv
filename = datetime.now().strftime('C:/Users/username/Desktop/Output/Canvas_data_%Y%m%d_%H%M.csv')
with open(filename, 'w', newline='', encoding="utf-8") as csvfile:
wr = csv.writer(csvfile)
wr.writerow([Canvas_script_data])
print("Canvas data is saved. ")
time.sleep(3)
The code was executed successfully and the data was saved in the csv file. However, everything was saved in the beginning cell of the csv file, which is not what I want.
Current CSV output:
Desired CSV Output: (Data is made up for viewing. Header is not necessary)
Script element snippet from the web page:
<script>
var plotActions = null;
$J(document).ready( function() {
$J.jqplot.config.enablePlugins = true;
plotActions = $J.jqplot(
'actions_graph'
, [ [['2019-02-18',27],['2019-02-19',42],['2019-02-20',39],['2019-02-21',40],['2019-02-22',37],['2019-02-23',11],['2019-02-24',28],['2019-02-25',37],['2019-02-26',36],['2019-02-27',39],['2019-02-28',36],['2019-03-01',27],['2019-03-02',13],['2019-03-03',24],['2019-03-04',27],['2019-03-05',27],['2019-03-06',30],['2019-03-07',32],['2019-03-08',19],['2019-03-09',7],['2019-03-10',11],['2019-03-11',39],['2019-03-12',36],['2019-03-13',30],['2019-03-14',28],['2019-03-15',23],['2019-03-16',19],['2019-03-17',16],['2019-03-18',28],['2019-03-19',29],['2019-03-20',28],['2019-03-21',28],['2019-03-22',22],['2019-03-23',16],['2019-03-24',21],['2019-03-25',30],['2019-03-26',35],['2019-03-27',30],['2019-03-28',22],['2019-03-29',17],['2019-03-30',7],['2019-03-31',14],['2019-04-01',26],['2019-04-02',30],['2019-04-03',26],['2019-04-04',27],['2019-04-05',24],['2019-04-06',5],['2019-04-07',15],['2019-04-08',30],['2019-04-09',32],['2019-04-10',35],['2019-04-11',36],['2019-04-12',28],['2019-04-13',6],['2019-04-14',21],['2019-04-15',33],['2019-04-16',32],['2019-04-17',30],['2019-04-18',31],['2019-04-19',16],['2019-04-20',4],['2019-04-21',3],['2019-04-22',35],['2019-04-23',34],['2019-04-24',24],['2019-04-25',28],['2019-04-26',27],['2019-04-27',4],['2019-04-28',15],['2019-04-29',27],['2019-04-30',29],['2019-05-01',26],['2019-05-02',29],['2019-05-03',27],['2019-05-04',27],['2019-05-05',35],['2019-05-06',44],['2019-05-07',49],['2019-05-08',48],['2019-05-09',43],['2019-05-10',29],['2019-05-11',11],['2019-05-12',16],['2019-05-13',25],['2019-05-14',26],['2019-05-15',23],['2019-05-16',28],['2019-05-17',28],['2019-05-18',8],['2019-05-19',16],['2019-05-20',29],['2019-05-21',28],['2019-05-22',25],['2019-05-23',29],['2019-05-24',25],['2019-05-25',3],['2019-05-26',19],['2019-05-27',16],['2019-05-28',38],['2019-05-29',37],['2019-05-30',32],['2019-05-31',26],['2019-06-01',11],['2019-06-02',21],['2019-06-03',31],['2019-06-04',36],['2019-06-05',32],['2019-06-06',35],['2019-06-07',36],['2019-06-08',15],['2019-06-09',15],['2019-06-10',36],['2019-06-11',42],['2019-06-12',38],['2019-06-13',37],['2019-06-14',25],['2019-06-15',5],['2019-06-16',15],['2019-06-17',31],['2019-06-18',30],['2019-06-19',122],['2019-06-20',68],['2019-06-21',41],['2019-06-22',28],['2019-06-23',32],['2019-06-24',51],['2019-06-25',41],['2019-06-26',38],['2019-06-27',127],['2019-06-28',167],['2019-06-29',107],['2019-06-30',113],['2019-07-01',88],['2019-07-02',63],['2019-07-03',70],['2019-07-04',66],['2019-07-05',60],['2019-07-06',37],['2019-07-07',45],['2019-07-08',40],['2019-07-09',43],['2019-07-10',42],['2019-07-11',56],['2019-07-12',38],['2019-07-13',32],['2019-07-14',27],['2019-07-15',49],['2019-07-16',200],['2019-07-17',166],['2019-07-18',174],['2019-07-19',160],['2019-07-20',212],['2019-07-21',153],['2019-07-22',120],['2019-07-23',92],['2019-07-24',80],['2019-07-25',97],['2019-07-26',88],['2019-07-27',71],['2019-07-28',79],['2019-07-29',67],['2019-07-30',63],['2019-07-31',62],['2019-08-01',70],['2019-08-02',75],['2019-08-03',67],['2019-08-04',60],['2019-08-05',49],['2019-08-06',56],['2019-08-07',164],['2019-08-08',252],['2019-08-09',282],['2019-08-10',254],['2019-08-11',248],['2019-08-12',97],['2019-08-13',74],['2019-08-14',77],['2019-08-15',68],['2019-08-16',56],['2019-08-17',53],['2019-08-18',64],['2019-08-19',52],['2019-08-20',68],['2019-08-21',68],['2019-08-22',57],['2019-08-23',59],['2019-08-24',57],['2019-08-25',43],['2019-08-26',48],['2019-08-27',81],['2019-08-28',94],['2019-08-29',79],['2019-08-30',55],['2019-08-31',49],['2019-09-01',71],['2019-09-02',55],['2019-09-03',53],['2019-09-04',53],['2019-09-05',185],['2019-09-06',291],['2019-09-07',227],['2019-09-08',193],['2019-09-09',149],['2019-09-10',128],['2019-09-11',156],['2019-09-12',125],['2019-09-13',131],['2019-09-14',107],['2019-09-15',110],['2019-09-16',127],['2019-09-17',83],['2019-09-18',100],['2019-09-19',95],['2019-09-20',134],['2019-09-21',121],['2019-09-22',95],['2019-09-23',99],['2019-09-24',102],['2019-09-25',91],['2019-09-26',87],['2019-09-27',113],['2019-09-28',103],['2019-09-29',87],['2019-09-30',104],['2019-10-01',102],['2019-10-02',114],['2019-10-03',105],['2019-10-04',97],['2019-10-05',77],['2019-10-06',74],['2019-10-07',85],['2019-10-08',80],['2019-10-09',80],['2019-10-10',83],['2019-10-11',108],['2019-10-12',101],['2019-10-13',92],['2019-10-14',95],['2019-10-15',98],['2019-10-16',143],['2019-10-17',215],['2019-10-18',179],['2019-10-19',171],['2019-10-20',173],['2019-10-21',174],['2019-10-22',150],['2019-10-23',139],['2019-10-24',113],['2019-10-25',121],['2019-10-26',122],['2019-10-27',111],['2019-10-28',122],['2019-10-29',129],['2019-10-30',233],['2019-10-31',246],['2019-11-01',339],['2019-11-02',420],['2019-11-03',360],['2019-11-04',294],['2019-11-05',275],['2019-11-06',273],['2019-11-07',348],['2019-11-08',390],['2019-11-09',434],['2019-11-10',454],['2019-11-11',370],['2019-11-12',67],['2019-11-13',48],['2019-11-14',21797],['2019-11-15',20594],['2019-11-16',26163],['2019-11-17',26767],['2019-11-18',19596],['2019-11-19',17971],['2019-11-20',17496],['2019-11-21',16814],['2019-11-22',17379],['2019-11-23',22385],['2019-11-24',23201],['2019-11-25',16796],['2019-11-26',15748],['2019-11-27',15553],['2019-11-28',15583],['2019-11-29',16391],['2019-11-30',20010],['2019-12-01',20315],['2019-12-02',15120],['2019-12-03',14207],['2019-12-04',13556],['2019-12-05',13329],['2019-12-06',14002],['2019-12-07',18413],['2019-12-08',18893],['2019-12-09',14361],['2019-12-10',13507],['2019-12-11',12669],['2019-12-12',12599],['2019-12-13',12842],['2019-12-14',16528],['2019-12-15',17187],['2019-12-16',13497],['2019-12-17',12987],['2019-12-18',12538],['2019-12-19',13140],['2019-12-20',13673],['2019-12-21',16574],['2019-12-22',17512],['2019-12-23',15182],['2019-12-24',15226],['2019-12-25',15318],['2019-12-26',15949],['2019-12-27',17080],['2019-12-28',18555],['2019-12-29',18749],['2019-12-30',17517],['2019-12-31',16773],['2020-01-01',18128],['2020-01-02',16780],['2020-01-03',17131],['2020-01-04',19131],['2020-01-05',18753],['2020-01-06',14837],['2020-01-07',14083],['2020-01-08',13636],['2020-01-09',14466],['2020-01-10',14837],['2020-01-11',17580],['2020-01-12',18262],['2020-01-13',14365],['2020-01-14',14017],['2020-01-15',13439],['2020-01-16',13368],['2020-01-17',14352],['2020-01-18',16666],['2020-01-19',17765],['2020-01-20',15138],['2020-01-21',13650],['2020-01-22',13878],['2020-01-23',13607],['2020-01-24',14790],['2020-01-25',18542],['2020-01-26',21513],['2020-01-27',16510],['2020-01-28',15546],['2020-01-29',14386],['2020-01-30',13865],['2020-01-31',15228],['2020-02-01',19386],['2020-02-02',20566],['2020-02-03',14739],['2020-02-04',13977],['2020-02-05',13501],['2020-02-06',13376],['2020-02-07',14526],['2020-02-08',19080],['2020-02-09',20924],['2020-02-10',14681],['2020-02-11',14147],['2020-02-12',13494],['2020-02-13',13093],['2020-02-14',13656],['2020-02-15',18519],['2020-02-16',19191]],[['2019-02-18',39],['2019-02-19',69],['2019-02-20',69],['2019-02-21',69],['2019-02-22',60],['2019-02-23',19],['2019-02-24',24],['2019-02-25',61],['2019-02-26',62],['2019-02-27',66],['2019-02-28',64],['2019-03-01',52],['2019-03-02',7],['2019-03-03',17],['2019-03-04',53],['2019-03-05',45],['2019-03-06',50],['2019-03-07',58],['2019-03-08',58],['2019-03-09',26],['2019-03-10',10],['2019-03-11',62],['2019-03-12',70],['2019-03-13',67],['2019-03-14',56],['2019-03-15',88],['2019-03-16',84],['2019-03-17',69],['2019-03-18',88],['2019-03-19',98],['2019-03-20',79],['2019-03-21',85],['2019-03-22',90],['2019-03-23',46],['2019-03-24',37],['2019-03-25',83],['2019-03-26',66],['2019-03-27',73],['2019-03-28',63],['2019-03-29',56],['2019-03-30',12],['2019-03-31',18],['2019-04-01',53],['2019-04-02',60],['2019-04-03',52],['2019-04-04',46],['2019-04-05',47],['2019-04-06',14],['2019-04-07',11],['2019-04-08',53],['2019-04-09',58],['2019-04-10',55],['2019-04-11',56],['2019-04-12',51],['2019-04-13',17],['2019-04-14',11],['2019-04-15',50],['2019-04-16',66],['2019-04-17',60],['2019-04-18',55],['2019-04-19',37],['2019-04-20',20],['2019-04-21',4],['2019-04-22',36],['2019-04-23',57],['2019-04-24',55],['2019-04-25',38],['2019-04-26',42],['2019-04-27',4],['2019-04-28',7],['2019-04-29',38],['2019-04-30',45],['2019-05-01',71],['2019-05-02',59],['2019-05-03',59],['2019-05-04',18],['2019-05-05',28],['2019-05-06',49],['2019-05-07',52],['2019-05-08',60],['2019-05-09',57],['2019-05-10',56],['2019-05-11',19],['2019-05-12',11],['2019-05-13',50],['2019-05-14',54],['2019-05-15',54],['2019-05-16',60],['2019-05-17',54],['2019-05-18',12],['2019-05-19',8],['2019-05-20',50],['2019-05-21',53],['2019-05-22',55],['2019-05-23',64],['2019-05-24',61],['2019-05-25',25],['2019-05-26',13],['2019-05-27',33],['2019-05-28',82],['2019-05-29',91],['2019-05-30',77],['2019-05-31',60],['2019-06-01',22],['2019-06-02',17],['2019-06-03',70],['2019-06-04',73],['2019-06-05',70],['2019-06-06',59],['2019-06-07',73],['2019-06-08',38],['2019-06-09',17],['2019-06-10',50],['2019-06-11',67],['2019-06-12',66],['2019-06-13',66],['2019-06-14',65],['2019-06-15',29],['2019-06-16',20],['2019-06-17',67],['2019-06-18',64],['2019-06-19',434],['2019-06-20',674],['2019-06-21',450],['2019-06-22',387],['2019-06-23',288],['2019-06-24',272],['2019-06-25',283],['2019-06-26',246],['2019-06-27',239],['2019-06-28',2029],['2019-06-29',1531],['2019-06-30',1182],['2019-07-01',877],['2019-07-02',740],['2019-07-03',717],['2019-07-04',638],['2019-07-05',496],['2019-07-06',511],['2019-07-07',482],['2019-07-08',423],['2019-07-09',391],['2019-07-10',361],['2019-07-11',420],['2019-07-12',486],['2019-07-13',410],['2019-07-14',318],['2019-07-15',306],['2019-07-16',1127],['2019-07-17',2297],['2019-07-18',1693],['2019-07-19',2153],['2019-07-20',2900],['2019-07-21',1993],['2019-07-22',1511],['2019-07-23',1286],['2019-07-24',1184],['2019-07-25',1182],['2019-07-26',1177],['2019-07-27',1072],['2019-07-28',949],['2019-07-29',765],['2019-07-30',715],['2019-07-31',721],['2019-08-01',720],['2019-08-02',983],['2019-08-03',997],['2019-08-04',811],['2019-08-05',690],['2019-08-06',630],['2019-08-07',1493],['2019-08-08',2557],['2019-08-09',3317],['2019-08-10',2635],['2019-08-11',2197],['2019-08-12',1556],['2019-08-13',1074],['2019-08-14',868],['2019-08-15',968],['2019-08-16',855],['2019-08-17',862],['2019-08-18',800],['2019-08-19',776],['2019-08-20',805],['2019-08-21',674],['2019-08-22',672],['2019-08-23',673],['2019-08-24',660],['2019-08-25',612],['2019-08-26',461],['2019-08-27',706],['2019-08-28',1239],['2019-08-29',772],['2019-08-30',677],['2019-08-31',746],['2019-09-01',670],['2019-09-02',547],['2019-09-03',501],['2019-09-04',484],['2019-09-05',802],['2019-09-06',3429],['2019-09-07',2974],['2019-09-08',2273],['2019-09-09',1585],['2019-09-10',1374],['2019-09-11',1512],['2019-09-12',1216],['2019-09-13',1276],['2019-09-14',1556],['2019-09-15',1313],['2019-09-16',1074],['2019-09-17',978],['2019-09-18',895],['2019-09-19',853],['2019-09-20',1476],['2019-09-21',1914],['2019-09-22',1385],['2019-09-23',1038],['2019-09-24',952],['2019-09-25',865],['2019-09-26',837],['2019-09-27',1253],['2019-09-28',1596],['2019-09-29',1151],['2019-09-30',955],['2019-10-01',1011],['2019-10-02',828],['2019-10-03',840],['2019-10-04',824],['2019-10-05',801],['2019-10-06',743],['2019-10-07',642],['2019-10-08',613],['2019-10-09',600],['2019-10-10',674],['2019-10-11',1230],['2019-10-12',1579],['2019-10-13',1112],['2019-10-14',865],['2019-10-15',865],['2019-10-16',998],['2019-10-17',2298],['2019-10-18',2351],['2019-10-19',2273],['2019-10-20',1823],['2019-10-21',1498],['2019-10-22',1383],['2019-10-23',1235],['2019-10-24',1121],['2019-10-25',1078],['2019-10-26',1557],['2019-10-27',1309],['2019-10-28',1055],['2019-10-29',1066],['2019-10-30',1806],['2019-10-31',2874],['2019-11-01',2790],['2019-11-02',3890],['2019-11-03',3285],['2019-11-04',2401],['2019-11-05',2327],['2019-11-06',2151],['2019-11-07',3262],['2019-11-08',3078],['2019-11-09',3690],['2019-11-10',3443],['2019-11-11',2458],['2019-11-12',251],['2019-11-13',189],['2019-11-14',87740],['2019-11-15',150768],['2019-11-16',156701],['2019-11-17',157231],['2019-11-18',126351],['2019-11-19',119994],['2019-11-20',117682],['2019-11-21',112477],['2019-11-22',115850],['2019-11-23',134639],['2019-11-24',139747],['2019-11-25',107884],['2019-11-26',106623],['2019-11-27',104459],['2019-11-28',103000],['2019-11-29',104391],['2019-11-30',121904],['2019-12-01',127855],['2019-12-02',100161],['2019-12-03',95283],['2019-12-04',90511],['2019-12-05',89266],['2019-12-06',92368],['2019-12-07',107838],['2019-12-08',114832],['2019-12-09',91524],['2019-12-10',87120],['2019-12-11',84438],['2019-12-12',82551],['2019-12-13',84522],['2019-12-14',98574],['2019-12-15',104069],['2019-12-16',84518],['2019-12-17',84277],['2019-12-18',87386],['2019-12-19',85482],['2019-12-20',87617],['2019-12-21',98574],['2019-12-22',104993],['2019-12-23',95928],['2019-12-24',90925],['2019-12-25',89104],['2019-12-26',96088],['2019-12-27',99387],['2019-12-28',105741],['2019-12-29',110093],['2019-12-30',101027],['2019-12-31',93747],['2020-01-01',99556],['2020-01-02',97742],['2020-01-03',98756],['2020-01-04',107851],['2020-01-05',109279],['2020-01-06',89815],['2020-01-07',83166],['2020-01-08',82225],['2020-01-09',83871],['2020-01-10',85831],['2020-01-11',100665],['2020-01-12',105059],['2020-01-13',82918],['2020-01-14',79973],['2020-01-15',78661],['2020-01-16',77721],['2020-01-17',81342],['2020-01-18',96219],['2020-01-19',99335],['2020-01-20',81454],['2020-01-21',79597],['2020-01-22',81523],['2020-01-23',82231],['2020-01-24',91734],['2020-01-25',111038],['2020-01-26',121034],['2020-01-27',104488],['2020-01-28',97650],['2020-01-29',93298],['2020-01-30',89784],['2020-01-31',94882],['2020-02-01',109628],['2020-02-02',114769],['2020-02-03',90262],['2020-02-04',88580],['2020-02-05',87972],['2020-02-06',87637],['2020-02-07',92375],['2020-02-08',108257],['2020-02-09',115316],['2020-02-10',90461],['2020-02-11',88754],['2020-02-12',86682],['2020-02-13',85726],['2020-02-14',85822],['2020-02-15',99929],['2020-02-16',107185]] ]
, {
animate: true,
animateReplot: true,
cursor: {
show: true,
zoom: true,
looseZoom: true,
clickReset: true,
showTooltip: false,
showTooltipUnitPosition: false
},
series:[ { label: 'Peak Concurrent Users'
, color: '#038080'
, highlighter: { formatString: 'Peak Concurrent Users: %s, %s'} , pointLabels: {
show: false,
markerOptions: { style: 'diamond' }, ypadding: 1,
}
, yaxis: 'yaxis'
, renderer: $J.jqplot.LineRenderer
, showHighlight: true , rendererOptions: {
animation: { speed: 2000 }
, highlightMouseOver: true
}
},
{ label: 'Daily Active Users'
, color: '#8a5706'
, highlighter: { formatString: 'Daily Active Users: %s, %s'} , pointLabels: {
show: false,
markerOptions: { style: 'diamond' }, ypadding: 1,
}
, yaxis: 'yaxis'
, renderer: $J.jqplot.LineRenderer
, showHighlight: true , rendererOptions: {
animation: { speed: 2000 }
, highlightMouseOver: true
}
},
],
seriesDefaults: {
showMarker: false,
lineWidth: 1.5 },
grid: {
backgroundColor: '#242424',
gridLineColor: '#424242',
borderColor: '#424242'
},
rendererOptions: {
showDataLabels: true
},
AxesDefaults: { pad: 0 },
axes: {
xaxis: {
renderer: $J.jqplot.DateAxisRenderer,
rendererOptions:{ tickRenderer: $J.jqplot.CanvasAxisTickRenderer },
numberTicks: 5,
tickOptions: {
formatString: '%b-%d-%Y', fontSize: '10pt'
},
min: '2019-01-30', max: '2020-03-06', drawMajorGridlines: true,
drawMinorGridlines: true,
drawMajorTickMarks: true,
rendererOptions: {
tickInset: 0.5,
minorTicks: 1,
sortMergedLabels: true
}
},
yaxis: { renderer: $J.jqplot.LinearAxisRenderer, rendererOptions: { forceTickAt0: true },labelRenderer: $J.jqplot.CanvasAxisLabelRenderer, label: '',min: 0, tickRenderer: $J.jqplot.CanvasAxisTickRenderer, tickOptions: { formatString: "%'i",fontSize: '10pt', showGridline: true }, pad: 1.01 } },
highlighter: {
show: true,
showLabel: true,
tooltipAxes: 'xy',
sizeAdjust: 2 ,
tooltipLocation : 'n',
tooltipOffset: 10,
lineWidthAdjust: 3,
useAxesFormatters: true
},
legend: {
renderer: $J.jqplot.EnhancedLegendRenderer
, show: true
, placement: 'outsideGrid'
, location: 's'
, fontSize: '10pt'
, marginBottom: '10px'
, rendererOptions: {
numberColumns: null,
numberRows: 1
}
},
});
});
</script>
You can try using string functions such as strip(), slice(), split() to clean out the data from the json block then run a python iterator to read and write data to a new csv file.
You have it as string so you can use standard functions for strings: split(), strip(), slicing [start:end], etc.
BTW: I use eval() or module dirtyjson to convert it because standard json makes problem beacuse string is not correct JSON data.
text = '''<script> ... </script>'''
lines = text.splitlines() # create list of lines
line = lines[10] # get one line
line = line[14:] # remove some chars
data = eval(line) # convert to list
#import json
#data = json.loads(line) # ERROR
#import dirtyjson
#data = dirtyjson.loads(line) # WORKS
print(data[0][0])
print(data[1][0])
Results
['2019-02-18', 27]
['2019-02-18', 39]
You have two lists data[0] and data[0] with values
['2019-02-18', 27] # from first list
['2019-02-18', 39] # from second list
which you can convert to
['2019-02-18', 27, 39]
and later to DataFrame using
import pandas as pd
new_data = []
for a, b in zip(data[0], data[1]):
if a[0] != b[0]:
print('Error', a, b)
else:
new_data.append([a[0], a[1], b[1]])
df = pd.DataFrame(new_data)
print(df)
Result:
0 1 2
0 2019-02-18 27 39
1 2019-02-19 42 69
2 2019-02-20 39 69
3 2019-02-21 40 69
4 2019-02-22 37 60
.. ... ... ...
359 2020-02-12 13494 86682
360 2020-02-13 13093 85726
361 2020-02-14 13656 85822
362 2020-02-15 18519 99929
363 2020-02-16 19191 107185
[364 rows x 3 columns]
Full working code:
text = '''<script>
var plotActions = null;
$J(document).ready( function() {
$J.jqplot.config.enablePlugins = true;
plotActions = $J.jqplot(
'actions_graph'
, [ [['2019-02-18',27],['2019-02-19',42],['2019-02-20',39],['2019-02-21',40],['2019-02-22',37],['2019-02-23',11],['2019-02-24',28],['2019-02-25',37],['2019-02-26',36],['2019-02-27',39],['2019-02-28',36],['2019-03-01',27],['2019-03-02',13],['2019-03-03',24],['2019-03-04',27],['2019-03-05',27],['2019-03-06',30],['2019-03-07',32],['2019-03-08',19],['2019-03-09',7],['2019-03-10',11],['2019-03-11',39],['2019-03-12',36],['2019-03-13',30],['2019-03-14',28],['2019-03-15',23],['2019-03-16',19],['2019-03-17',16],['2019-03-18',28],['2019-03-19',29],['2019-03-20',28],['2019-03-21',28],['2019-03-22',22],['2019-03-23',16],['2019-03-24',21],['2019-03-25',30],['2019-03-26',35],['2019-03-27',30],['2019-03-28',22],['2019-03-29',17],['2019-03-30',7],['2019-03-31',14],['2019-04-01',26],['2019-04-02',30],['2019-04-03',26],['2019-04-04',27],['2019-04-05',24],['2019-04-06',5],['2019-04-07',15],['2019-04-08',30],['2019-04-09',32],['2019-04-10',35],['2019-04-11',36],['2019-04-12',28],['2019-04-13',6],['2019-04-14',21],['2019-04-15',33],['2019-04-16',32],['2019-04-17',30],['2019-04-18',31],['2019-04-19',16],['2019-04-20',4],['2019-04-21',3],['2019-04-22',35],['2019-04-23',34],['2019-04-24',24],['2019-04-25',28],['2019-04-26',27],['2019-04-27',4],['2019-04-28',15],['2019-04-29',27],['2019-04-30',29],['2019-05-01',26],['2019-05-02',29],['2019-05-03',27],['2019-05-04',27],['2019-05-05',35],['2019-05-06',44],['2019-05-07',49],['2019-05-08',48],['2019-05-09',43],['2019-05-10',29],['2019-05-11',11],['2019-05-12',16],['2019-05-13',25],['2019-05-14',26],['2019-05-15',23],['2019-05-16',28],['2019-05-17',28],['2019-05-18',8],['2019-05-19',16],['2019-05-20',29],['2019-05-21',28],['2019-05-22',25],['2019-05-23',29],['2019-05-24',25],['2019-05-25',3],['2019-05-26',19],['2019-05-27',16],['2019-05-28',38],['2019-05-29',37],['2019-05-30',32],['2019-05-31',26],['2019-06-01',11],['2019-06-02',21],['2019-06-03',31],['2019-06-04',36],['2019-06-05',32],['2019-06-06',35],['2019-06-07',36],['2019-06-08',15],['2019-06-09',15],['2019-06-10',36],['2019-06-11',42],['2019-06-12',38],['2019-06-13',37],['2019-06-14',25],['2019-06-15',5],['2019-06-16',15],['2019-06-17',31],['2019-06-18',30],['2019-06-19',122],['2019-06-20',68],['2019-06-21',41],['2019-06-22',28],['2019-06-23',32],['2019-06-24',51],['2019-06-25',41],['2019-06-26',38],['2019-06-27',127],['2019-06-28',167],['2019-06-29',107],['2019-06-30',113],['2019-07-01',88],['2019-07-02',63],['2019-07-03',70],['2019-07-04',66],['2019-07-05',60],['2019-07-06',37],['2019-07-07',45],['2019-07-08',40],['2019-07-09',43],['2019-07-10',42],['2019-07-11',56],['2019-07-12',38],['2019-07-13',32],['2019-07-14',27],['2019-07-15',49],['2019-07-16',200],['2019-07-17',166],['2019-07-18',174],['2019-07-19',160],['2019-07-20',212],['2019-07-21',153],['2019-07-22',120],['2019-07-23',92],['2019-07-24',80],['2019-07-25',97],['2019-07-26',88],['2019-07-27',71],['2019-07-28',79],['2019-07-29',67],['2019-07-30',63],['2019-07-31',62],['2019-08-01',70],['2019-08-02',75],['2019-08-03',67],['2019-08-04',60],['2019-08-05',49],['2019-08-06',56],['2019-08-07',164],['2019-08-08',252],['2019-08-09',282],['2019-08-10',254],['2019-08-11',248],['2019-08-12',97],['2019-08-13',74],['2019-08-14',77],['2019-08-15',68],['2019-08-16',56],['2019-08-17',53],['2019-08-18',64],['2019-08-19',52],['2019-08-20',68],['2019-08-21',68],['2019-08-22',57],['2019-08-23',59],['2019-08-24',57],['2019-08-25',43],['2019-08-26',48],['2019-08-27',81],['2019-08-28',94],['2019-08-29',79],['2019-08-30',55],['2019-08-31',49],['2019-09-01',71],['2019-09-02',55],['2019-09-03',53],['2019-09-04',53],['2019-09-05',185],['2019-09-06',291],['2019-09-07',227],['2019-09-08',193],['2019-09-09',149],['2019-09-10',128],['2019-09-11',156],['2019-09-12',125],['2019-09-13',131],['2019-09-14',107],['2019-09-15',110],['2019-09-16',127],['2019-09-17',83],['2019-09-18',100],['2019-09-19',95],['2019-09-20',134],['2019-09-21',121],['2019-09-22',95],['2019-09-23',99],['2019-09-24',102],['2019-09-25',91],['2019-09-26',87],['2019-09-27',113],['2019-09-28',103],['2019-09-29',87],['2019-09-30',104],['2019-10-01',102],['2019-10-02',114],['2019-10-03',105],['2019-10-04',97],['2019-10-05',77],['2019-10-06',74],['2019-10-07',85],['2019-10-08',80],['2019-10-09',80],['2019-10-10',83],['2019-10-11',108],['2019-10-12',101],['2019-10-13',92],['2019-10-14',95],['2019-10-15',98],['2019-10-16',143],['2019-10-17',215],['2019-10-18',179],['2019-10-19',171],['2019-10-20',173],['2019-10-21',174],['2019-10-22',150],['2019-10-23',139],['2019-10-24',113],['2019-10-25',121],['2019-10-26',122],['2019-10-27',111],['2019-10-28',122],['2019-10-29',129],['2019-10-30',233],['2019-10-31',246],['2019-11-01',339],['2019-11-02',420],['2019-11-03',360],['2019-11-04',294],['2019-11-05',275],['2019-11-06',273],['2019-11-07',348],['2019-11-08',390],['2019-11-09',434],['2019-11-10',454],['2019-11-11',370],['2019-11-12',67],['2019-11-13',48],['2019-11-14',21797],['2019-11-15',20594],['2019-11-16',26163],['2019-11-17',26767],['2019-11-18',19596],['2019-11-19',17971],['2019-11-20',17496],['2019-11-21',16814],['2019-11-22',17379],['2019-11-23',22385],['2019-11-24',23201],['2019-11-25',16796],['2019-11-26',15748],['2019-11-27',15553],['2019-11-28',15583],['2019-11-29',16391],['2019-11-30',20010],['2019-12-01',20315],['2019-12-02',15120],['2019-12-03',14207],['2019-12-04',13556],['2019-12-05',13329],['2019-12-06',14002],['2019-12-07',18413],['2019-12-08',18893],['2019-12-09',14361],['2019-12-10',13507],['2019-12-11',12669],['2019-12-12',12599],['2019-12-13',12842],['2019-12-14',16528],['2019-12-15',17187],['2019-12-16',13497],['2019-12-17',12987],['2019-12-18',12538],['2019-12-19',13140],['2019-12-20',13673],['2019-12-21',16574],['2019-12-22',17512],['2019-12-23',15182],['2019-12-24',15226],['2019-12-25',15318],['2019-12-26',15949],['2019-12-27',17080],['2019-12-28',18555],['2019-12-29',18749],['2019-12-30',17517],['2019-12-31',16773],['2020-01-01',18128],['2020-01-02',16780],['2020-01-03',17131],['2020-01-04',19131],['2020-01-05',18753],['2020-01-06',14837],['2020-01-07',14083],['2020-01-08',13636],['2020-01-09',14466],['2020-01-10',14837],['2020-01-11',17580],['2020-01-12',18262],['2020-01-13',14365],['2020-01-14',14017],['2020-01-15',13439],['2020-01-16',13368],['2020-01-17',14352],['2020-01-18',16666],['2020-01-19',17765],['2020-01-20',15138],['2020-01-21',13650],['2020-01-22',13878],['2020-01-23',13607],['2020-01-24',14790],['2020-01-25',18542],['2020-01-26',21513],['2020-01-27',16510],['2020-01-28',15546],['2020-01-29',14386],['2020-01-30',13865],['2020-01-31',15228],['2020-02-01',19386],['2020-02-02',20566],['2020-02-03',14739],['2020-02-04',13977],['2020-02-05',13501],['2020-02-06',13376],['2020-02-07',14526],['2020-02-08',19080],['2020-02-09',20924],['2020-02-10',14681],['2020-02-11',14147],['2020-02-12',13494],['2020-02-13',13093],['2020-02-14',13656],['2020-02-15',18519],['2020-02-16',19191]],[['2019-02-18',39],['2019-02-19',69],['2019-02-20',69],['2019-02-21',69],['2019-02-22',60],['2019-02-23',19],['2019-02-24',24],['2019-02-25',61],['2019-02-26',62],['2019-02-27',66],['2019-02-28',64],['2019-03-01',52],['2019-03-02',7],['2019-03-03',17],['2019-03-04',53],['2019-03-05',45],['2019-03-06',50],['2019-03-07',58],['2019-03-08',58],['2019-03-09',26],['2019-03-10',10],['2019-03-11',62],['2019-03-12',70],['2019-03-13',67],['2019-03-14',56],['2019-03-15',88],['2019-03-16',84],['2019-03-17',69],['2019-03-18',88],['2019-03-19',98],['2019-03-20',79],['2019-03-21',85],['2019-03-22',90],['2019-03-23',46],['2019-03-24',37],['2019-03-25',83],['2019-03-26',66],['2019-03-27',73],['2019-03-28',63],['2019-03-29',56],['2019-03-30',12],['2019-03-31',18],['2019-04-01',53],['2019-04-02',60],['2019-04-03',52],['2019-04-04',46],['2019-04-05',47],['2019-04-06',14],['2019-04-07',11],['2019-04-08',53],['2019-04-09',58],['2019-04-10',55],['2019-04-11',56],['2019-04-12',51],['2019-04-13',17],['2019-04-14',11],['2019-04-15',50],['2019-04-16',66],['2019-04-17',60],['2019-04-18',55],['2019-04-19',37],['2019-04-20',20],['2019-04-21',4],['2019-04-22',36],['2019-04-23',57],['2019-04-24',55],['2019-04-25',38],['2019-04-26',42],['2019-04-27',4],['2019-04-28',7],['2019-04-29',38],['2019-04-30',45],['2019-05-01',71],['2019-05-02',59],['2019-05-03',59],['2019-05-04',18],['2019-05-05',28],['2019-05-06',49],['2019-05-07',52],['2019-05-08',60],['2019-05-09',57],['2019-05-10',56],['2019-05-11',19],['2019-05-12',11],['2019-05-13',50],['2019-05-14',54],['2019-05-15',54],['2019-05-16',60],['2019-05-17',54],['2019-05-18',12],['2019-05-19',8],['2019-05-20',50],['2019-05-21',53],['2019-05-22',55],['2019-05-23',64],['2019-05-24',61],['2019-05-25',25],['2019-05-26',13],['2019-05-27',33],['2019-05-28',82],['2019-05-29',91],['2019-05-30',77],['2019-05-31',60],['2019-06-01',22],['2019-06-02',17],['2019-06-03',70],['2019-06-04',73],['2019-06-05',70],['2019-06-06',59],['2019-06-07',73],['2019-06-08',38],['2019-06-09',17],['2019-06-10',50],['2019-06-11',67],['2019-06-12',66],['2019-06-13',66],['2019-06-14',65],['2019-06-15',29],['2019-06-16',20],['2019-06-17',67],['2019-06-18',64],['2019-06-19',434],['2019-06-20',674],['2019-06-21',450],['2019-06-22',387],['2019-06-23',288],['2019-06-24',272],['2019-06-25',283],['2019-06-26',246],['2019-06-27',239],['2019-06-28',2029],['2019-06-29',1531],['2019-06-30',1182],['2019-07-01',877],['2019-07-02',740],['2019-07-03',717],['2019-07-04',638],['2019-07-05',496],['2019-07-06',511],['2019-07-07',482],['2019-07-08',423],['2019-07-09',391],['2019-07-10',361],['2019-07-11',420],['2019-07-12',486],['2019-07-13',410],['2019-07-14',318],['2019-07-15',306],['2019-07-16',1127],['2019-07-17',2297],['2019-07-18',1693],['2019-07-19',2153],['2019-07-20',2900],['2019-07-21',1993],['2019-07-22',1511],['2019-07-23',1286],['2019-07-24',1184],['2019-07-25',1182],['2019-07-26',1177],['2019-07-27',1072],['2019-07-28',949],['2019-07-29',765],['2019-07-30',715],['2019-07-31',721],['2019-08-01',720],['2019-08-02',983],['2019-08-03',997],['2019-08-04',811],['2019-08-05',690],['2019-08-06',630],['2019-08-07',1493],['2019-08-08',2557],['2019-08-09',3317],['2019-08-10',2635],['2019-08-11',2197],['2019-08-12',1556],['2019-08-13',1074],['2019-08-14',868],['2019-08-15',968],['2019-08-16',855],['2019-08-17',862],['2019-08-18',800],['2019-08-19',776],['2019-08-20',805],['2019-08-21',674],['2019-08-22',672],['2019-08-23',673],['2019-08-24',660],['2019-08-25',612],['2019-08-26',461],['2019-08-27',706],['2019-08-28',1239],['2019-08-29',772],['2019-08-30',677],['2019-08-31',746],['2019-09-01',670],['2019-09-02',547],['2019-09-03',501],['2019-09-04',484],['2019-09-05',802],['2019-09-06',3429],['2019-09-07',2974],['2019-09-08',2273],['2019-09-09',1585],['2019-09-10',1374],['2019-09-11',1512],['2019-09-12',1216],['2019-09-13',1276],['2019-09-14',1556],['2019-09-15',1313],['2019-09-16',1074],['2019-09-17',978],['2019-09-18',895],['2019-09-19',853],['2019-09-20',1476],['2019-09-21',1914],['2019-09-22',1385],['2019-09-23',1038],['2019-09-24',952],['2019-09-25',865],['2019-09-26',837],['2019-09-27',1253],['2019-09-28',1596],['2019-09-29',1151],['2019-09-30',955],['2019-10-01',1011],['2019-10-02',828],['2019-10-03',840],['2019-10-04',824],['2019-10-05',801],['2019-10-06',743],['2019-10-07',642],['2019-10-08',613],['2019-10-09',600],['2019-10-10',674],['2019-10-11',1230],['2019-10-12',1579],['2019-10-13',1112],['2019-10-14',865],['2019-10-15',865],['2019-10-16',998],['2019-10-17',2298],['2019-10-18',2351],['2019-10-19',2273],['2019-10-20',1823],['2019-10-21',1498],['2019-10-22',1383],['2019-10-23',1235],['2019-10-24',1121],['2019-10-25',1078],['2019-10-26',1557],['2019-10-27',1309],['2019-10-28',1055],['2019-10-29',1066],['2019-10-30',1806],['2019-10-31',2874],['2019-11-01',2790],['2019-11-02',3890],['2019-11-03',3285],['2019-11-04',2401],['2019-11-05',2327],['2019-11-06',2151],['2019-11-07',3262],['2019-11-08',3078],['2019-11-09',3690],['2019-11-10',3443],['2019-11-11',2458],['2019-11-12',251],['2019-11-13',189],['2019-11-14',87740],['2019-11-15',150768],['2019-11-16',156701],['2019-11-17',157231],['2019-11-18',126351],['2019-11-19',119994],['2019-11-20',117682],['2019-11-21',112477],['2019-11-22',115850],['2019-11-23',134639],['2019-11-24',139747],['2019-11-25',107884],['2019-11-26',106623],['2019-11-27',104459],['2019-11-28',103000],['2019-11-29',104391],['2019-11-30',121904],['2019-12-01',127855],['2019-12-02',100161],['2019-12-03',95283],['2019-12-04',90511],['2019-12-05',89266],['2019-12-06',92368],['2019-12-07',107838],['2019-12-08',114832],['2019-12-09',91524],['2019-12-10',87120],['2019-12-11',84438],['2019-12-12',82551],['2019-12-13',84522],['2019-12-14',98574],['2019-12-15',104069],['2019-12-16',84518],['2019-12-17',84277],['2019-12-18',87386],['2019-12-19',85482],['2019-12-20',87617],['2019-12-21',98574],['2019-12-22',104993],['2019-12-23',95928],['2019-12-24',90925],['2019-12-25',89104],['2019-12-26',96088],['2019-12-27',99387],['2019-12-28',105741],['2019-12-29',110093],['2019-12-30',101027],['2019-12-31',93747],['2020-01-01',99556],['2020-01-02',97742],['2020-01-03',98756],['2020-01-04',107851],['2020-01-05',109279],['2020-01-06',89815],['2020-01-07',83166],['2020-01-08',82225],['2020-01-09',83871],['2020-01-10',85831],['2020-01-11',100665],['2020-01-12',105059],['2020-01-13',82918],['2020-01-14',79973],['2020-01-15',78661],['2020-01-16',77721],['2020-01-17',81342],['2020-01-18',96219],['2020-01-19',99335],['2020-01-20',81454],['2020-01-21',79597],['2020-01-22',81523],['2020-01-23',82231],['2020-01-24',91734],['2020-01-25',111038],['2020-01-26',121034],['2020-01-27',104488],['2020-01-28',97650],['2020-01-29',93298],['2020-01-30',89784],['2020-01-31',94882],['2020-02-01',109628],['2020-02-02',114769],['2020-02-03',90262],['2020-02-04',88580],['2020-02-05',87972],['2020-02-06',87637],['2020-02-07',92375],['2020-02-08',108257],['2020-02-09',115316],['2020-02-10',90461],['2020-02-11',88754],['2020-02-12',86682],['2020-02-13',85726],['2020-02-14',85822],['2020-02-15',99929],['2020-02-16',107185]] ]
, {
animate: true,
animateReplot: true,
cursor: {
show: true,
zoom: true,
looseZoom: true,
clickReset: true,
showTooltip: false,
showTooltipUnitPosition: false
},
series:[ { label: 'Peak Concurrent Users'
, color: '#038080'
, highlighter: { formatString: 'Peak Concurrent Users: %s, %s'} , pointLabels: {
show: false,
markerOptions: { style: 'diamond' }, ypadding: 1,
}
, yaxis: 'yaxis'
, renderer: $J.jqplot.LineRenderer
, showHighlight: true , rendererOptions: {
animation: { speed: 2000 }
, highlightMouseOver: true
}
},
{ label: 'Daily Active Users'
, color: '#8a5706'
, highlighter: { formatString: 'Daily Active Users: %s, %s'} , pointLabels: {
show: false,
markerOptions: { style: 'diamond' }, ypadding: 1,
}
, yaxis: 'yaxis'
, renderer: $J.jqplot.LineRenderer
, showHighlight: true , rendererOptions: {
animation: { speed: 2000 }
, highlightMouseOver: true
}
},
],
seriesDefaults: {
showMarker: false,
lineWidth: 1.5 },
grid: {
backgroundColor: '#242424',
gridLineColor: '#424242',
borderColor: '#424242'
},
rendererOptions: {
showDataLabels: true
},
AxesDefaults: { pad: 0 },
axes: {
xaxis: {
renderer: $J.jqplot.DateAxisRenderer,
rendererOptions:{ tickRenderer: $J.jqplot.CanvasAxisTickRenderer },
numberTicks: 5,
tickOptions: {
formatString: '%b-%d-%Y', fontSize: '10pt'
},
min: '2019-01-30', max: '2020-03-06', drawMajorGridlines: true,
drawMinorGridlines: true,
drawMajorTickMarks: true,
rendererOptions: {
tickInset: 0.5,
minorTicks: 1,
sortMergedLabels: true
}
},
yaxis: { renderer: $J.jqplot.LinearAxisRenderer, rendererOptions: { forceTickAt0: true },labelRenderer: $J.jqplot.CanvasAxisLabelRenderer, label: '',min: 0, tickRenderer: $J.jqplot.CanvasAxisTickRenderer, tickOptions: { formatString: "%'i",fontSize: '10pt', showGridline: true }, pad: 1.01 } },
highlighter: {
show: true,
showLabel: true,
tooltipAxes: 'xy',
sizeAdjust: 2 ,
tooltipLocation : 'n',
tooltipOffset: 10,
lineWidthAdjust: 3,
useAxesFormatters: true
},
legend: {
renderer: $J.jqplot.EnhancedLegendRenderer
, show: true
, placement: 'outsideGrid'
, location: 's'
, fontSize: '10pt'
, marginBottom: '10px'
, rendererOptions: {
numberColumns: null,
numberRows: 1
}
},
});
});
</script>'''
lines = text.splitlines() # create list of lines
line = lines[10] # get one line
line = line[14:] # remove some chars
data = eval(line) # convert to list
#import json
#data = json.loads(line) # ERROR
import dirtyjson
data = dirtyjson.loads(line) # WORKS
print(data[0][0])
print(data[1][0])
new_data = []
for a, b in zip(data[0], data[1]):
if a[0] != b[0]:
print('Error', a, b)
else:
new_data.append([a[0], a[1], b[1]])
import pandas as pd
df = pd.DataFrame(new_data)
print(df)

How to add title row to using writer and unicodecsv

I have the following JSON file - test.json (names, keys and addresses changed for security reasons)
[
{
"accountMode":"Live",
"acquirer":"TEST",
"acquirerConstraints":{
"cardTypes":[
"MASTERCARD",
"MAESTRO",
"VISA"
],
"cvcRegexp":"^[0-9]{3}$",
"cvcRequired":true,
"maxAmount":500000,
"minAmount":50
},
"acquirerDetails":{
"TEST":"Studio",
"ERROR_LIST":[
],
"MERCHANT_CODE":"218331",
"VALID":true,
"_mId":"T712484",
"_status":"INPROCESS",
"email":"test7#gmail.com",
"name":"Studio",
"valid":true
},
"acquirerValidations":null,
"allowedCurrencies":[
"EUR",
"USD",
"GBP"
],
"apiKeyPairs":[
{
"accountMode":"Live",
"label":"Virtual Terminal",
"publishableKey":"niunibiubniunijknkjknj",
"source":"VIRTUAL_TERMINAL"
},
{
"accountMode":"Live",
"label":"Default",
"publishableKey":"iiuhiuhiu",
"source":"ECOMMERCE"
}
],
"appLogoUrl":null,
"applicationId":"541d75e0-7db8b343a31f",
"authorizationCode":"",
"closedDate":null,
"closureReason":null,
"declineAvsAddressFailure":false,
"declineAvsZipFailure":false,
"declineCvcFailure":false,
"defaultCurrency":"EUR",
"descriptor":null,
"email":"test1#gmail.com",
"id":"ddddeff",
"invitationCode":null,
"locale":"en_IE",
"merchantApplication":{
"accountNumber":null,
"acquirer":"TEST",
"annualAmount":null,
"annualVolume":null,
"applicationType":"APPROVAL",
"bankName":"UNKNOWN",
"brand":null,
"businessAddress":"54 My St, 1",
"businessAddress2":null,
"businessCity":"Abbey",
"businessCountry":"IRL",
"businessPhone":null,
"businessState":"DUBLIN",
"businessZip":null,
"data":null,
"email":"test#gmail.com",
"escalationPhone":null,
"fax":null,
"legalName":"UAB \"Studio\"",
"maxTransactionAmount":null,
"mccCode":"5712",
"merchantPromotionCode":null,
"mobile":null,
"monthlyAmount":null,
"monthlyVolume":null,
"ownerFirstName":"tlana",
"ownerLastName":"nava",
"phone":"37647",
"GuideAccepted":null,
"privacyAccepted":true,
"privacyVersion":"1a",
"referenceId":"9104d65i08d071",
"routingNumber":null,
"singleTransactionAmount":null,
"statementName":"UAB \"Studio\"",
"taxId":null,
"termsAccepted":true,
"termsVersion":"1a",
"url":"http://www.design.lt"
},
"merchantId":"12484",
"merchantPromotionCode":null,
"mposEnabled":true,
"name":"Studio",
"netonfiguration":null,
"onboardedDate":1505513232485,
"onboardingMethod":null,
"onboardingStatus":"INPROCESS",
"partner":null,
"saqCompliant":false,
"saqExpires":null,
"settings":[
{
"key":"MERCHANT_DETAILS",
"value":"{\"zip\":\"Wicklow\",\"phone\":\"342647\",\"email\":\"suppoor#outlook.com\",\"address\":\"Bck 6\",\"state\":\"Ireland\",\"addressLine2\":\"Unit 8, Bl Par\",\"city\":\"Wicklow\"}"
},
{
"key":"VAT_NUMBER",
"value":"/evzaqen/"
}
],
"timezone":"Europe/Dublin",
"tinStatus":null
},
{
"accountMode":"Live",
"acquirer":"TEST",
"acquirerConstraints":{
"cardTypes":[
"MASTERCARD",
"MAESTRO",
"VISA"
],
"cvcRegexp":"^[0-9]{3}$",
"cvcRequired":true,
"maxAmount":500000,
"minAmount":50
},
"acquirerDetails":{
"TEST":"test",
"ERROR_LIST":[
],
"MERCHANT_CODE":"594920",
"MID_ASSIGNED":true,
"VALID":true,
"_mId":"103558",
"_status":"APPROVED",
"acquiringMid":"1036598",
"descriptor":"test 8885551212",
"email":"test#gmail.com",
"gatewayMid":"SIMP337",
"id":"SIMP337",
"level4Mid":"76576576",
"name":"test",
"status":"APPROVED",
"transactionCurrency":"USD;EUR;GBP",
"valid":true,
"paymentGatewayKey":"ytfytfytfyt"
},
"acquirerValidations":null,
"allowedCurrencies":[
"EUR",
"USD",
"GBP"
],
"apiKeyPairs":[
],
"appLogoUrl":null,
"applicationId":"949bdde5-07-d8d58f4c3d01",
"authorizationCode":"",
"closedDate":null,
"closureReason":null,
"declineAvsAddressFailure":false,
"declineAvsZipFailure":false,
"declineCvcFailure":false,
"defaultCurrency":"EUR",
"descriptor":"test85551212",
"email":"test#gmail.com",
"id":"9f3a7d7",
"invitationCode":null,
"locale":"en_US",
"merchantApplication":{
"accountNumber":null,
"acquirer":"TEST",
"annualAmount":null,
"annualVolume":null,
"applicationType":"APPROVAL",
"bankName":"UNKNOWN",
"brand":null,
"businessAddress":"123 test",
"businessAddress2":null,
"businessCity":"Atlanta",
"businessCountry":"IRL",
"businessPhone":null,
"businessState":"CARLOW",
"businessZip":null,
"data":null,
"email":"test#gmail.com",
"escalationPhone":null,
"fax":null,
"legalName":"stest",
"maxTransactionAmount":null,
"mccCode":"521",
"merchantPromotionCode":null,
"mobile":null,
"monthlyAmount":null,
"monthlyVolume":null,
"ownerFirstName":"moto",
"ownerLastName":"test",
"phone":"3141212",
"GuideAccepted":null,
"privacyAccepted":true,
"privacyVersion":"1a",
"referenceId":"2920",
"routingNumber":null,
"singleTransactionAmount":null,
"statementName":"test",
"taxId":null,
"termsAccepted":true,
"termsVersion":"1a",
"url":null
},
"merchantId":"1036558",
"merchantPromotionCode":null,
"mposEnabled":true,
"name":"test",
"netonfiguration":null,
"onboardedDate":1456846054925,
"onboardingMethod":null,
"onboardingStatus":"CLOSED",
"partner":null,
"saqCompliant":false,
"saqExpires":null,
"settings":[
],
"timezone":"Europe/Dublin",
"tinStatus":"InCompliance"
}
]
I want to process this file and take some of the information and populate a CSV file with it. To do this I am using the following:
import unicodecsv
import json
json_data = open("test.json")
data = json.load(json_data)
f = unicodecsv.writer(open("results.csv","wb+"))
for entry in data:
if "merchantApplication" in entry:
ma = entry["merchantApplication"]
if "email" in ma:
f.writerow([ma["ownerFirstName"],ma["ownerLastName"],ma["email"],ma["legalName"],ma["businessAddress"],ma["businessAddress2"],ma["businessCity"],ma["businessCountry"],ma["businessState"],ma["businessZip"],ma["phone"],ma["mobile"]])
json_data.close()
This Works fine but does not print the headers above the columns. How do I add in the headers? I am using Python 2.7.10
How do I add in the headers?
Well quite simply by calling f.writerow((<your>,<headers>,<here>)) before your for loop.

Flatten the Json file data using pandas normalizer

I would like to flatten the complex nested json file .Please find the below sample json data
{
"applications": [
{
"id": 87334412,
"name": "cdata1",
"language": "known",
"health_status": "unknown",
"reporting": true,
"last_reported_at": "2017-10-06T06:30:55+00:00",
"application_summary": {
"response_time": 1.2,
"throughput": 216,
"error_rate": 0,
"target": 0.5,
"ascore": 1,
"host_count": 3,
"instance_count": 3
},
"settings": {
"column": 0.5,
"columns": 7,
"columns1": true,
"columns2": false
},
"links": {
"application_data": [
93818199,
93819351,
93819359
],
"servers": [],
"application_content": [
32006189,
87342924,
47565225
]
}
},
code using :
import json
from pandas.io.json import json_normalize
json_file=open('ptr1.json')
json_data=json.load(json_file)
#print json_data["applications"]
for line in json_data:
data=json_normalize(line,['name','id'])
print data
can any one help to get the following data name,id,last_reported_at,instance_count. note json file contains many id details
IIUC:
In [34]: d = json.loads(json_str)
In [35]: cols = ['id','name','last_reported_at','application_summary.instance_count']
In [36]: pd.io.json.json_normalize(d['applications'])[cols]
Out[36]:
id name last_reported_at application_summary.instance_count
0 87334412 cdata1 2017-10-06T06:30:55+00:00 3
1 87334444 cdata2 2017-10-05T06:30:55+00:00 3

Parsing Through Nested JSON Python

data = response.json()
sortJson = json.dumps(data, sort_keys=True,
indent=2, separators=(',', ':'))
result = json.loads (data)
print ('"saleTotal":', result['trips']['tripOption']['pricing']['saleTotal'])
This is the code I have currently. I'm looking to parse through a nested JSON file, but each time I run this I get the following error:
TypeError: the JSON object must be str, not 'dict'
The JSON file when Pretty Printed turns out like this:
{
"kind":"qpxExpress#tripsSearch",
"trips":{
"data":{
"aircraft":[
{
"code":"321",
"kind":"qpxexpress#aircraftData",
"name":"Airbus A321"
}
],
"airport":[
{
"city":"ORL",
"code":"MCO",
"kind":"qpxexpress#airportData",
"name":"Orlando International"
},
{
"city":"CHI",
"code":"ORD",
"kind":"qpxexpress#airportData",
"name":"Chicago O'Hare"
}
],
"carrier":[
{
"code":"F9",
"kind":"qpxexpress#carrierData",
"name":"Frontier Airlines, Inc."
}
],
"city":[
{
"code":"CHI",
"kind":"qpxexpress#cityData",
"name":"Chicago"
},
{
"code":"ORL",
"kind":"qpxexpress#cityData",
"name":"Orlando"
}
],
"kind":"qpxexpress#data",
"tax":[
{
"id":"ZP",
"kind":"qpxexpress#taxData",
"name":"US Flight Segment Tax"
},
{
"id":"AY_001",
"kind":"qpxexpress#taxData",
"name":"US September 11th Security Fee"
},
{
"id":"US_001",
"kind":"qpxexpress#taxData",
"name":"US Transportation Tax"
},
{
"id":"XF",
"kind":"qpxexpress#taxData",
"name":"US Passenger Facility Charge"
}
]
},
"kind":"qpxexpress#tripOptions",
"requestId":"2z1TQ9iVMcSlUH8HW0O0eq",
"tripOption":[
{
"id":"WQZ8ICu2L8RLqt1MyMNFAQ001",
"kind":"qpxexpress#tripOption",
"pricing":[
{
"baseFareTotal":"USD37.11",
"fare":[
{
"basisCode":"Z00ZSS5",
"carrier":"F9",
"destination":"ORL",
"id":"AR5um4n2cToXHml3a125O0CU7toTISvPQER/01Xhbf2E",
"kind":"qpxexpress#fareInfo",
"origin":"CHI"
}
],
"fareCalculation":"ORD F9 MCO Q9.29 Q4.65 23.17Z00ZSS5 USD 37.11 END ZP ORD XT 2.79US 4.00ZP 5.60AY 4.50XF ORD4.50",
"kind":"qpxexpress#pricingInfo",
"latestTicketingTime":"2016-03-22T00:24-04:00",
"passengers":{
"adultCount":1,
"kind":"qpxexpress#passengerCounts"
},
"ptc":"ADT",
"saleFareTotal":"USD37.11",
"saleTaxTotal":"USD16.89",
"saleTotal":"USD54.00",
"segmentPricing":[
{
"fareId":"AR5um4n2cToXHml3a125O0CU7toTISvPQER/01Xhbf2E",
"kind":"qpxexpress#segmentPricing",
"segmentId":"GoIDkawPBE2TZk14"
}
],
"tax":[
{
"chargeType":"GOVERNMENT",
"code":"US",
"country":"US",
"id":"US_001",
"kind":"qpxexpress#taxInfo",
"salePrice":"USD2.79"
},
{
"chargeType":"GOVERNMENT",
"code":"AY",
"country":"US",
"id":"AY_001",
"kind":"qpxexpress#taxInfo",
"salePrice":"USD5.60"
},
{
"chargeType":"GOVERNMENT",
"code":"XF",
"country":"US",
"id":"XF",
"kind":"qpxexpress#taxInfo",
"salePrice":"USD4.50"
},
{
"chargeType":"GOVERNMENT",
"code":"ZP",
"country":"US",
"id":"ZP",
"kind":"qpxexpress#taxInfo",
"salePrice":"USD4.00"
}
]
}
],
"saleTotal":"USD54.00",
"slice":[
{
"duration":167,
"kind":"qpxexpress#sliceInfo",
"segment":[
{
"bookingCode":"Z",
"bookingCodeCount":9,
"cabin":"COACH",
"duration":167,
"flight":{
"carrier":"F9",
"number":"1294"
},
"id":"GoIDkawPBE2TZk14",
"kind":"qpxexpress#segmentInfo",
"leg":[
{
"aircraft":"321",
"arrivalTime":"2016-05-11T09:42-04:00",
"departureTime":"2016-05-11T05:55-05:00",
"destination":"MCO",
"duration":167,
"id":"LQKIza3yQIpaLyDq",
"kind":"qpxexpress#legInfo",
"meal":"Food and Beverages for Purchase",
"mileage":1006,
"origin":"ORD",
"originTerminal":"3",
"secure":true
}
],
"marriedSegmentGroup":"0"
}
]
}
]
}
]
}
}
Ultimately I'm trying to find the saleTotal and have the corresponding value printed alongside it:
saleTotal: 54.00
tripOption and pricing are lists of objects, if you take that into account it will work:
print ('"saleTotal":', result['trips']['tripOption'][0]['pricing'][0]['saleTotal'])
Output:
"saleTotal": USD54.00

Categories