如何从python查询列表中创建json数组

时间:2016-05-05 23:11:52

标签: python sql json oracle

我需要为我正在执行的每个语句生成一个json对象。这将在以后用于在网页上提供一些表格和图表,但是现在我很难生成4个json数组。任何帮助是极大的赞赏。到目前为止,这是我的代码:

import json
import requests
import collections
import cx_Oracle as ora
import csv

print "Content-type: application/json"

statements = []

## Prepare umts weekly site_level cqi statement
umts_cqi_site_stmt = """SELECT PERIOD_END, ZONE_ID, USID, SITE_ID, ENGINEERING_NOTES, SITE_RANK, U_CQI_SCORE, 
                        TOTAL_3GCQI, U_DROPPED_CALL_RATE, ACCESSIBILITY, U_PS_RETAINABILITY, PS_RAN_ACCESSIBILITY, U_RAN_THROUGHPUT
                        FROM moks.umts_weekly_cqi_drilldown"""

## Prepare lte weekly site_level cqi statement
lte_cqi_site_stmt = """SELECT PERIOD_END, ZONE_ID, USID, SITE_ID, ENGINEERING_NOTES, SITE_RANK, LCQI_SCORE, TOTAL_LCQI, 
                       TNOL_CSFB, ERAB_RETANABILITY, RAN_DATA_ACCESSIBILITY, IRAT, DL_UE_THROUGHPUT
                       FROM moks.lte_weekly_cqi_drilldown"""

## Prepare volte weekly site_level cqi statement
volte_cqi_site_stmt = """SELECT PERIOD_END, ZONE_ID, USID, SITE_ID, ENGINEERING_NOTES, SITE_RANK, VOLTE_SCORE, TOTAL_VOLTECQI, 
                         DROP_CALLS_CORE_CDR, CNOV_CALLS_NOT_ON_VOLTE, ACCESS_FAILURES_RAN_NODE_STATS, DROP_CALLS_RAN_NODE_STATS
                         FROM moks.volte_weekly_cqi_drilldown"""                

## Prepare weekly market cqi statement
cqi_market_weekly_stmt = """SELECT TO_CHAR(WEEK,'YYYY-MM-DD') WEEK, MARKET, METRICNAME, CQI, YELLOW_THRESHOLD, GREEN_THRESHOLD
                            FROM moks.market_level_cqi_weekly 
                            ORDER BY METRICNAME, WEEK, MARKET"""

statements = [umts_cqi_site_stmt,lte_cqi_site_stmt,volte_cqi_site_stmt,cqi_market_weekly_stmt]

connection = ora.connect('---------------------')
print "We are connected!"

cursor = connection.cursor()
for stmt in statements:
    cursor.execute(stmt)

rows = cursor.fetchall()

json_output = json.dumps(rows)
print json_output

print "done"

0 个答案:

没有答案