TA的每日心情 | 开心 2014-1-11 00:20 |
---|
签到天数: 38 天 [LV.5]常住居民I
|
楼主 |
发表于 2018-12-14 15:35:58
|
显示全部楼层
继续学习,不断熟悉python风格:
def DataCNex(num, Ttype = '', Ctype = ''):
'''
语法说明:
DataCNex(integer/float/'time struct', string, string) -> string
Ttype
一般供内部保留。参数意思:数据类型,但是主要的类型是根据第一个参数num去自动判断
='date',只输出单数字,不带“十百千万”等表位字
Ctype
参数意思:中文的大小写或者时间输出是否精确到时分秒
='uppercase',输出大写数字
='fulltime',输出年月日时分秒
在Ttype不特别说明的时候可以直接输入Ctype
例如
print (DataCNex(20008007012))
print (DataCNex(20008007012, 'uppercase'))
print (DataCNex(20008007012, 'date'))
print (DataCNex(20008007012, 'date', 'uppercase'))
print (DataCNex(200080.07012, 'uppercase'))
如果 import time
print (DataCNex(localtime()))
print (DataCNex(localtime(), 'fulltime'))
'''
if "uppercase" in Ttype or 'fulltime' in Ttype:
Ctype = Ttype
Ttype = ''
if 'uppercase' == Ctype:
M0 = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
P0 = ['', '拾','佰','仟','万','拾','佰','仟','亿','拾','佰','仟','万','拾万','佰万', '仟万']
else:
M0 = ['〇', '一', '二', '三', '四', '五', '六', '七', '八', '九']
P0 = ['', '十','百','千','万','十','百','千','亿','十','百','千','万','十万','百万', '千万']
if '' == Ttype:
Ttype = str(type(num))
if "int" in Ttype:
return DataCNex_d(num, M0, P0)
elif "date" in Ttype:
return DataCNex_p(int(num), M0)
elif "float" in Ttype:
d,p = (str(num).split('.'))
return DataCNex_d(int(d), M0, P0)+"点"+DataCNex_p(p, M0)
elif 'struct_time' in Ttype:
if 'fulltime' in Ctype:
return ("%s年%s月%s日,星期%s,%s时%s分%s秒" % \
(DataCNex_p(num.tm_year, M0), DataCNex_d(num.tm_mon, M0, P0), DataCNex_d(num.tm_mday, M0, P0), \
DataCNex_p(num.tm_wday + 1, M0), DataCNex_d(num.tm_hour, M0, P0), DataCNex_d(num.tm_min, M0, P0), \
DataCNex_d(num.tm_sec, M0, P0)))
else:
return ("%s年%s月%s日,星期%s" % \
(DataCNex_p(num.tm_year, M0), DataCNex_d(num.tm_mon, M0, P0), DataCNex_d(num.tm_mday, M0, P0), \
DataCNex_p(num.tm_wday + 1, M0)))
else:
return "错误:数据类型 %s,请输入整形数或者浮点数。" % str(type(num))
def DataCNex_d(num, _M0, _P0):
a_list = list(str(num))
a_len = len(a_list)
result =''
for i,aa, pp in zip(range(a_len), a_list, _P0[a_len-1::-1]):
if 0 != int(aa):
if a_len-2 == i and '1' == aa and 2 == a_len:
result += pp
else:
result += _M0[int(aa)] + pp
else:
if a_len-5 == i or a_len-9 == i:
result += pp
if a_len-1 != i and int(a_list[i+1]):
result += _M0[int(aa)]
return result
def DataCNex_p(num, _M0):
result = ''
for aa in list(str(num)):
result += _M0[int(aa)]
return result
if __name__ == "__main__":
print (DataCNex(20008007012))
print (DataCNex(10))
print (DataCNex(15))
print (DataCNex(20008007012, 'uppercase'))
print (DataCNex(20008007012, 'date'))
print (DataCNex(20008007012, 'date', 'uppercase'))
print (DataCNex(200080.07012, 'uppercase'))
print (DataCNex(200080.07012,'date', 'uppercase'))
print (DataCNex('test'))
|
|