日常工作中,我們難免會遇到要直連數(shù)據(jù)庫來進行數(shù)據(jù)的讀取與處理,然而會有幾種比較有特點的數(shù)據(jù)需要進行轉(zhuǎn)換,整理如下:
一、Decimal數(shù)據(jù)轉(zhuǎn)換
?????從數(shù)據(jù)庫中讀出來的值為Decimal('50'),轉(zhuǎn)換為字符串如下:
? ? ? ? ? ? from decimal import *
? ? ? ? ? ? s=Decimal('50')
? ? ? ? ? ?s_str =str(s.quantize(Decimal('0.00')))? #四舍五入保留兩位小數(shù)
二、datetime.datetime數(shù)據(jù)轉(zhuǎn)換
從數(shù)據(jù)庫中讀出來的值為datetime.datetime(2018, 6, 5, 11, 28, 49),轉(zhuǎn)換為字符串如下:
? ? ? ? ? ? date_data =datetime.datetime(2018, 6, 5, 11, 28, 49)
? ? ? ? ? ? date_str = date_data.strftime("%Y-%m-%d %T")? #轉(zhuǎn)換為字符串,精確到時分秒