I use pymssql library to query the result, here is one example:
('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)
But I found there are values like Decimal('120000000.00')
and datetime.datetime(2016, 1, 15, 0, 5, 51, 480000)
. I only want to get the raw value without Decimal() and datetime.datetime(). How can I implement this?
Here is my code:
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
The purpose of this function is to construct a JSON format file with attribute and value pairs, like:
{'Price': '120000000.00'}
instead of
{'Price': Decimal('120000000.00')}
You can pass a
default
function to thejson.dumps
method that convertsDecimal
to string, like this:The console output is:
Note that the function could return
float(obj)
instead ofstr(obj)
forDecimal
values, but a loss of precision might occur.