概述
1 #时间转中文
2
3 a = "20180509"
4 deftime_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 intime_info.items()}8 for i instime: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 deftime_text(stime):19 time_info = {'零': 0, '壹': 1, '贰': 2, '叁': 3, '肆': 4, '伍': 5, '陆': 6, '柒': 7, '捌': 8, '玖': 9, }20 new_dict = {v: k for k, v intime_info.items()}21 ret = [new_dict[int(i)] for i instime ]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 classcnumber: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 =g55 while k <=lx:56 csdata.append(cdata[k:k + 4])57 k += 4
58 returncsdata59
60 def cschange(self, cki): #对[亿,万,仟]的list中每个字符串分组进行大写化再合并
61 lenki =len(cki)62 i =063 lk =lenki64 chk = u''
65 for i inrange(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 returnchk74
75 defcwchange(self, data):76 cdata = str(data).split('.')77
78 cki =cdata[0]79 ckj = cdata[1]80 i =081 chk = u''
82 cski = self.csplit(cki) #分解字符数组[亿,万,仟]三组List:['0000','0000','0000']
83 ikl = len(cski) #获取拆分后的List长度
84 #大写合并
85 for i inrange(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 returnchk107
108
109 if __name__ == '__main__':110 pt =cnumber()111 print(pt.cwchange('2018.00'))
最后
以上就是腼腆跳跳糖为你收集整理的python数字转中文字符_python时间转换中文--------数字钱转换中文的全部内容,希望文章能够帮你解决python数字转中文字符_python时间转换中文--------数字钱转换中文所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复