python如何将阿拉伯数字转为汉子数字,1到99
在Python中如果想将阿拉伯数字转为汉子数字,是没有现成的函数的,只能自己写了,今天在写一个程序时需要用到,所以就写了一个1到10阿拉伯数字转为汉子数字,现在将源程序代码放在下面,供大家参考:
Python
import re
fo=open('基础/地址.txt',encoding='utf-8')
strs=fo.read()
strs=re.sub('\n','',strs)
str_list=strs.split(' ')
item={}
i=0
hanzi_list = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
imgnum = 1
i=0
while i<len(str_list):
# while i<110:
if imgnum // 10 == 0:
new_num_str = hanzi_list[imgnum - 1]
elif imgnum // 10 == 1:
if imgnum % 10 == 0:
new_num_str = '十'
else:
new_num_str = f'十{hanzi_list[imgnum % 10 - 1]}'
elif imgnum//10<10:
if imgnum % 10 == 0:
new_num_str = f'{hanzi_list[imgnum // 10 - 1]}十'
else:
new_num_str = f'{hanzi_list[imgnum // 10 - 1]}十{hanzi_list[imgnum % 10 - 1]}'
if i==0:
print("%s、%s"%(new_num_str,str_list[i+2]))
print("地址:%s"%str_list[i+3])
str_list[i+4]=re.sub('周六','\n周六',str_list[i+4])
print("工作时间:\n%s"%str_list[i+4])
i+=5
else:
print("%s、%s"%(new_num_str,str_list[i+1]))
print("地址:%s"%str_list[i+2])
str_list[i+3]=re.sub('周六','\n周六',str_list[i+3])
print("工作时间:\n%s"%str_list[i+3])
i+=4
imgnum+=1
输出结果如下图:
注意:如果超过100,逻辑上需要继续往下写,欢迎大家完善。
评论