python时间转换中文--------数字钱转换中文


  1 # 时间转中文
  2 
  3 a = "20180509"
  4 def time_text(stime):
  5     data_list = []
  6     time_info = {'': 0, '': 1, '': 2, '': 3, '': 4, '': 5, '': 6, '': 7, '': 8, '': 9, }
  7     new_dict = {v: k for k, v in time_info.items()}
  8     for i in stime:
  9         data_list.append(new_dict[int(i)])
 10     str_time = ''.join(data_list)
 11     print(str_time)
 12 time_text(a)
 13 
 14 
 15 # 时间转中文
 16 
 17 # 列表推倒式       20180509
 18 def time_text(stime):
 19     time_info = {'': 0, '': 1, '': 2, '': 3, '': 4, '': 5, '': 6, '': 7, '': 8, '': 9, }
 20     new_dict = {v: k for k, v in time_info.items()}
 21     ret = [new_dict[int(i)] for i in stime ]
 22     result = ''.join(ret)
 23     print(result)
 24 time_text(a)
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 #   钱转中文
 35 a = '2018'
 36 
 37 
 38 class cnumber:
 39     cdict = {}
 40     gdict = {}
 41     xdict = {}
 42 
 43     def __init__(self):
 44         self.cdict = {1: u'', 2: u'', 3: u'', 4: u''}
 45         self.xdict = {1: u'', 2: u'', 3: u'亿', 4: u''}  # 数字标识符
 46         self.gdict = {0: u'', 1: u'', 2: u'', 3: u'', 4: u'', 5: u'', 6: u'', 7: u'', 8: u'', 9: u''}
 47 
 48     def csplit(self, cdata):  # 拆分函数,将整数字符串拆分成[亿,万,仟]的list
 49         g = len(cdata) % 4
 50         csdata = []
 51         lx = len(cdata) - 1
 52         if g > 0:
 53             csdata.append(cdata[0:g])
 54         k = g
 55         while k <= lx:
 56             csdata.append(cdata[k:k + 4])
 57             k += 4
 58         return csdata
 59 
 60     def cschange(self, cki):  # 对[亿,万,仟]的list中每个字符串分组进行大写化再合并
 61         lenki = len(cki)
 62         i = 0
 63         lk = lenki
 64         chk = u''
 65         for i in range(lenki):
 66             if int(cki[i]) == 0:
 67                 if i < lenki - 1:
 68                     if int(cki[i + 1]) != 0:
 69                         chk = chk + self.gdict[int(cki[i])]
 70             else:
 71                 chk = chk + self.gdict[int(cki[i])] + self.cdict[lk]
 72             lk -= 1
 73         return chk
 74 
 75     def cwchange(self, data):
 76         cdata = str(data).split('.')
 77 
 78         cki = cdata[0]
 79         ckj = cdata[1]
 80         i = 0
 81         chk = u''
 82         cski = self.csplit(cki)  # 分解字符数组[亿,万,仟]三组List:['0000','0000','0000']
 83         ikl = len(cski)  # 获取拆分后的List长度
 84         # 大写合并
 85         for i in range(ikl):
 86             if self.cschange(cski[i]) == '':  # 有可能一个字符串全是0的情况
 87                 chk = chk + self.cschange(cski[i])  # 此时不需要将数字标识符引入
 88             else:
 89                 chk = chk + self.cschange(cski[i]) + self.xdict[ikl - i]  # 合并:前字符串大写+当前字符串大写+标识符
 90         # 处理小数部分
 91         lenkj = len(ckj)
 92         if lenkj == 1:  # 若小数只有1位
 93             if int(ckj[0]) == 0:
 94                 chk = chk + u''
 95             else:
 96                 chk = chk + self.gdict[int(ckj[0])] + u'角整'
 97         else:  # 若小数有两位的四种情况
 98             if int(ckj[0]) == 0 and int(ckj[1]) != 0:
 99                 chk = chk + u'' + self.gdict[int(ckj[1])] + u''
100             elif int(ckj[0]) == 0 and int(ckj[1]) == 0:
101                 chk = chk + u''
102             elif int(ckj[0]) != 0 and int(ckj[1]) != 0:
103                 chk = chk + self.gdict[int(ckj[0])] + u'' + self.gdict[int(ckj[1])] + u''
104             else:
105                 chk = chk + self.gdict[int(ckj[0])] + u'角整'
106         return chk
107 
108 
109 if __name__ == '__main__':
110     pt = cnumber()
111     print(pt.cwchange('2018.00'))

 

# 一行搞定,时间转换中文


a = "3435465765"
ret = a.replace("0","").replace("1","").replace("2","").replace("3","").replace("4","").replace("5","").replace("6","").replace("7","").replace("8","").replace("9","")
print(ret) 


#    转换钱

a = {"0": "", "1": "", "2": "", "3": "", "4": "", "5": "", "6": "", "7": "", "8": "", "9": ""}
b = {"0": "", "1": "", "2": "", "3": "", "4": "", "5": "", "6": "", "7": "", "8": "亿"}
c= '120180509'
print("".join([a[i] for i in c]))
d = []
length = len(c)
for i, v in enumerate(c):
    d.append(a[v])
    if v != "0":
        d.append(b[str(length - (i + 1))])
print("".join(d))

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM