pymssql如何删除查询结果中的数据类型

pymssql how to remove data type in queried result

我使用pymssql库查询结果,这里是一个例子:

('999001', '4871C9DF-6E95-458B-B763-6BBBB6C75F5D', 1, True, 2, 'EOI', 'Dropped', None, None, 4, False, False, 'Enbloc', False, False, False, False, None, None, 'USD', None, None, None, None, None, None, 'Investment', None, None, None, 'JLL Deal', 'Exclusive Mandated', 'Sales Advisory', None, None, None, None, None, 'Korea Standard Time', None, None, None, None, None, None, None, None, None, 1, None, True, 'En-bloc', False, False, False, False, False, False, None, '1', None, None, None, None, None, None, Decimal('0.05'), '100%', None, None, None, None, None, 'Sqft', 'USD', Decimal('120000000.00'), 'USD$ 120.0M', Decimal('120000000.00'), 'USD$ 120.0M', None, None, None, None, None, None, None, None, None, None, Decimal('120000000.00'), Decimal('120000000.00'), Decimal('120000000.00'), Decimal('120000000.00'), 'Undisclosed', 'Hubville Co Ltd', 'An Exciting Investment Opportunity - The Gateway to Seoul', None, None, None, None, 'An Exciting Investment Opportunity - The Gateway to Seoul', None, None, datetime.datetime(2016, 1, 15, 0, 5, 51, 480000), datetime.datetime(2016, 12, 7, 5, 6, 52, 633000), 1, None)

但我发现有 Decimal('120000000.00')datetime.datetime(2016, 1, 15, 0, 5, 51, 480000) 这样的值。我只想获得没有 Decimal() 和 datetime.datetime() 的原始值。我该如何实施?

这是我的代码:

def extract_opportunities(sql):
    cursor.execute(sql)
    opportunities = cursor.fetchall()
    attributes = get_attributes_of_table(cursor)
    availability_id_idx = attributes.index("availabilityid")
    opportunity_dict = dict()
    for idx, val in enumerate(opportunities):
        opportunity_key = val[availability_id_idx]
        opportunity_value = dict(zip(attributes, val))
        opportunity_dict[opportunity_key] = opportunity_value

这个函数的目的是构造一个JSON格式文件,其中包含属性和值对,例如:

{'Price': '120000000.00'} 

而不是

{'Price': Decimal('120000000.00')}

您可以将 default 函数传递给将 Decimal 转换为字符串的 json.dumps 方法,如下所示:

import datetime
from decimal import Decimal
import json
import pymssql

def json_dumps_default(obj):
    # ref: 
    if isinstance(obj, Decimal):
        return str(obj)
    if isinstance(obj, datetime.datetime):
        return str(obj)
    raise TypeError

conn = pymssql.connect(
    host=r'localhost:49242',
    database='myDb'
    )
crsr = conn.cursor(as_dict=True)
sql = """\
SELECT
    CONVERT(DECIMAL(18,4), 120000000) AS Price,
    CONVERT(DATETIME, '2016-12-07 05:06:52.633') AS SaleDate
UNION ALL
SELECT
    CONVERT(DECIMAL(18,4), 340000000) AS Price,
    CONVERT(DATETIME, '2017-01-09 07:44:32.1') AS SaleDate
"""
crsr.execute(sql)
rows = crsr.fetchall()
print()
print("Rows as returned by pymssql:")
print(rows)

as_json = json.dumps(rows, default=json_dumps_default, sort_keys=False, indent=2)
print()
print("Rows as returned by json.dumps:")
print(as_json)

crsr.close()
conn.close()

控制台输出为:

Rows as returned by pymssql:
[{'Price': Decimal('120000000.0000'), 'SaleDate': datetime.datetime(2016, 12, 7, 5, 6, 52, 633000)}, {'Price': Decimal('340000000.0000'), 'SaleDate': datetime.datetime(2017, 1, 9, 7, 44, 32, 100000)}]

Rows as returned by json.dumps:
[
  {
    "Price": "120000000.0000",
    "SaleDate": "2016-12-07 05:06:52.633000"
  },
  {
    "Price": "340000000.0000",
    "SaleDate": "2017-01-09 07:44:32.100000"
  }
]

请注意,对于 Decimal 值,该函数可以 return float(obj) 而不是 str(obj),但可能会丢失精度。