循环使用SQL表并使用Pyodbc

时间:2015-06-05 17:57:20

标签: python json dictionary

我有一个SQL表,我正在转换为具有更高级别信息的JSON对象,例如服务请求号,地址,纬度/经度等。

嵌套词典将始终是与较高级别词典相关的信息,例如商品类型,即(电子废物),项目类型,即(微波),收集的项目数,即(2)。

我有两个必需的密钥,以便我发送JSON的服务器接受请求。

我的问题是,如何成功遍历表格中的较低级别信息并将这些词典附加到我的更高级词典?这些词典中还有10个。一个SRNumber有一种商品类型。

我能够通过硬编码实现我的结果,如下所示,但我的内部词典是动态的,即ListofLA311ElectronicWaste可以很容易地ListofLA311BulkyItems与相关的内部词典。

我当前的输出:

{
        "MetaData": {}, 
        "SRData": {
            "ListOfLa311ElectronicWaste": {
                "La311ElectronicWaste": [
                    {
                        "DriverFirstName": "Bob", 
                        "DriverLastName": "Williams", 
                        "ItemCount": "2", 
                        "Type": "Electronic Waste"
                    }
                ]
            }, 
            "ReasonCode": "", 
            "ResolutionCode": "", 
            "SRNumber": "1-17991171"
        }
    }
]

代码:

import pyodbc
import json
import collections
import requests
import time
import logging
import httplib
import datetime
import logging
import logging.handlers



results = []
for row in cursor.fetchall():
    result = dict(zip(columns, row))
    SRNumber =  result['SRNumber']
    rescode = result ['ResolutionCode']
    results.append({'MetaData': {}, 'SRData': {'SRNumber':SRNumber,'ReasonCode':"",'ResolutionCode': rescode, ""'ListOfLa311ElectronicWaste' : {"La311ElectronicWaste" : [{'ItemCount': '2', 'Type': "Electronic Waste", 'DriverFirstName': "Bob", "DriverLastName": "Williams" }]}}})
    print json.dumps(results, sort_keys=True, indent=4)

期望的输出;

{
    "MetaData": {},
    "SRData": {
        "ListOfLa311ElectronicWaste": {
            "La311ElectronicWaste": [
                {
                    "DriverFirstName": "aDriverFirst",
                    "DriverLastName": "aDriverLast",
                    "ItemCount": "3",
                    "ElectronicWestType": " Microwave",
                    "Type": "Electronic Waste"
                },
                {
                    "DriverFirstName": "aDriverFirst",
                    "DriverLastName": "aDriverLast",
                    "ItemCount": " 10",
                    "ElectronicWasteType": "Television",
                    "Type": "Electronic Waste",
                    "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff"
                },
                {
                    "DriverFirstName": "aDriverFirst",
                    "DriverLastName": "aDriverLast",
                    "ItemCount": " 2",
                    "ElectronicWasteType": "Cell Phone",
                    "Type": "Electronic Waste",
                    "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff"
                },
                {
                    "DriverFirstName": "aDriverFirst",
                    "DriverLastName": "aDriverLast",
                    "ItemCount": " 7",
                    "ElectronicWasteType": "Monitors",
                    "Type": "Electronic Waste",
                    "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff"
                },
                {
                    "DriverFirstName": "aDriverFirst",
                    "DriverLastName": "aDriverLast",
                    "ItemCount": " 1",
                    "ElectronicWasteType": "Keyboards",
                    "Type": "Electronic Waste",
                    "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff"
                },
                {
                    "DriverFirstName": "aDriverFirst",
                    "DriverLastName": "aDriverLast",
                    "ItemCount": " 1",
                    "ElectronicWasteType": "ac adapter",
                    "Type": "Electronic Waste",
                    "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff"
                },
                {
                    "DriverFirstName": "aDriverFirst",
                    "DriverLastName": "aDriverLast",
                    "ItemCount": " 1",
                    "ElectronicWasteType": "headphones",
                    "Type": "Electronic Waste",
                    "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff"
                },
                {
                    "DriverFirstName": "aDriverFirst",
                    "DriverLastName": "aDriverLast",
                    "ItemCount": " 1",
                    "ElectronicWasteType": "generator",
                    "Type": "Electronic Waste",
                    "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff"
                },
                {
                    "DriverFirstName": "aDriverFirst",
                    "DriverLastName": "aDriverLast",
                    "ItemCount": " 1",
                    "ElectronicWasteType": "electric stapler",
                    "Type": "Electronic Waste",
                    "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff"
                },
                {
                    "DriverFirstName": "aDriverFirst",
                    "DriverLastName": "aDriverLast",
                    "ItemCount": " 1",
                    "ElectronicWasteType": "laptop",
                    "Type": "Electronic Waste",
                    "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff"
                },
                {
                    "DriverFirstName": "aDriverFirst",
                    "DriverLastName": "aDriverLast",
                    "ItemCount": " 1",
                    "ElectronicWasteType": "battery",
                    "Type": "Electronic Waste",
                    "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff"
                }
            ]
        },
        "ReasonCode": "",
        "ResolutionCode": "",
        "SRNumber": "1-17991171"
    }
}

1 个答案:

答案 0 :(得分:1)

并不是说它完全符合你的要求,但它应该让你开始。

使用大量 my_dictionary.get(键,默认)和可接受的缺省默认值来查找元数据字典中的内容将使您的代码更加健壮。当没有数据可以找到时,你也可以有条件地跳过东西。

还有一件事:有效的json数据有一些要求。请注意嵌入的字符串分隔符。JSON linter online在那里可能会有所帮助。此外,Python json.dumps不像其他一些Python json编码器库那样健壮,所以你可能想要如果在线解析器没问题,请寻找替代方案,但是你的json.dumps不是。

import json 

#create a mockup of your cursor data...
SRNumber_17991171 = "1-17991171"

li_simulate_cursor=[
    dict(ResolutionCode="", SRNumber=SRNumber_17991171)
]

#create a mockup of what the higher order data looks like
#http://jsonlint.com/ can be a help...

s17991171 = """{
                "name" : "ListOfLa311ElectronicWaste",
                "driverlist": 
                        [
                    {
                        "DriverFirstName": "aDriverFirst",
                        "DriverLastName": "aDriverLast",
                        "ItemCount": "3",
                        "ElectronicWestType": " Microwave",
                        "Type": "Electronic Waste"
                    },
                    {
                        "DriverFirstName": "aDriverFirst",
                        "DriverLastName": "aDriverLast",
                        "ItemCount": " 10",
                        "ElectronicWasteType": "Television",
                        "Type": "Electronic Waste",
                        "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff"
                    }          
                    ]
                }
"""

#this is totally bogus, a mockup, but you 
di_lookup_meta = {"1-17991171": json.loads(s17991171)}


#ok, now all done with the prep, let's proceed
results = []
for row in li_simulate_cursor:
    result = row
    SRNumber =  result['SRNumber']
    rescode = result ['ResolutionCode']

    try:
        #lookup your meta for the the cursor results
        meta = di_lookup_meta.get(SRNumber,"")

        #transform your higher order data to your desired output format
        name = meta.get("name")
        driverlist = meta.get("driverlist")
        MetaData = meta.get("MetaData", {})

        di_meta = {name:driverlist}

    except KeyError, e:
        print "not found:%s in %s" % (SRNumber, di_lookup_meta.keys())
        raise


    di_json = dict(
        MetaData = MetaData,
        SRData = dict(SRNumber=SRNumber,ResolutionCode=rescode),
        )

    #embedding your higher order
    di_json.update(di_meta)

    results.append(di_json)

    # results.append({'MetaData': {}, 'SRData': {'SRNumber':SRNumber,'ReasonCode':"",'ResolutionCode': rescode, ""'ListOfLa311ElectronicWaste' : {"La311ElectronicWaste" : [{'ItemCount': '2', 'Type': "Electronic Waste", 'DriverFirstName': "Bob", "DriverLastName": "Williams" }]}}})

print json.dumps(results, sort_keys=True, indent=4)

输出:

[
    {
        "ListOfLa311ElectronicWaste": [
            {
                "DriverFirstName": "aDriverFirst", 
                "DriverLastName": "aDriverLast", 
                "ElectronicWestType": " Microwave", 
                "ItemCount": "3", 
                "Type": "Electronic Waste"
            }, 
            {
                "DriverFirstName": "aDriverFirst", 
                "DriverLastName": "aDriverLast", 
                "ElectronicWasteType": "Television", 
                "ItemCount": " 10", 
                "Name": "c2d66d96-2439-4486-b4f0-5377b084cdff", 
                "Type": "Electronic Waste"
            }
        ], 
        "MetaData": {}, 
        "SRData": {
            "ResolutionCode": "", 
            "SRNumber": "1-17991171"
        }
    }
]