自己珍藏一些有趣的Python子程序
Posted 卓晴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自己珍藏一些有趣的Python子程序相关的知识,希望对你有一定的参考价值。
简 介: 这里收集了几个自己喜欢并且在之后有可能会被重复应用的Python小程序
关键词
: Python
§01 汉字数字
一、从零到99
下面子程序输出从0到99的数字的汉字数字字符串。
def chinesestr(num):
num1 = num // 10
num0 = num - num1*10
orderstr = '一二三四五六七八九十'
if num <= 0: return "零"
if num == 10: return "十"
if num < 10:
return orderstr[num-1]
if num < 20:
return "十"+orderstr[num0-1]
if num0 == 0:
return orderstr[num1-1] + '十'
return orderstr[num1-1] + '十' + orderstr[num0-1]
二、从零到999
def chinesestr99(num):
num1 = num // 10
num0 = num - num1*10
orderstr = '一二三四五六七八九十'
if num <= 0: return "零"
if num == 10: return "十"
if num < 10:
return orderstr[num-1]
if num < 20:
return "十"+orderstr[num0-1]
if num0 == 0:
return orderstr[num1-1] + '十'
return orderstr[num1-1] + '十' + orderstr[num0-1]
def chinesestr999(num):
orderstr = '一二三四五六七八九十'
if num < 100: return chinesestr99(num)
if (num%100) == 0: return orderstr[num//100-1]+'百'
if (num%100) < 10: return orderstr[num//100-1] + "百零" + orderstr[(num%10)-1]
num3 = num//100
num2 = (num%100)//10
num1 = num%10
if num1 > 0: return '%s百%s十%s'%(orderstr[num3-1], orderstr[num2-1], orderstr[num1-1])
else: return '%s百%s十'%(orderstr[num3-1], orderstr[num2-1])
二、标题设置批处理
下面小程序对于CSDN编辑器中的标题进行批处理。
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TEST1.PY -- by Dr. ZhuoQing 2021-08-17
#
# Note:
#============================================================
from head import *
csdn_window = '写文章-CSDN博客'
tspsendwindowkey(csdn_window, "c", control=1)
strall = [s for s in clipboard.paste().split('\\n') if len(s) > 0]
printf(strall)
#------------------------------------------------------------
def chinesestr99(num):
num1 = num // 10
num0 = num - num1*10
orderstr = '一二三四五六七八九十'
if num <= 0: return "零"
if num == 10: return "十"
if num < 10:
return orderstr[num-1]
if num < 20:
return "十"+orderstr[num0-1]
if num0 == 0:
return orderstr[num1-1] + '十'
return orderstr[num1-1] + '十' + orderstr[num0-1]
def chinesestr999(num):
orderstr = '一二三四五六七八九十'
if num < 100: return chinesestr99(num)
if (num%100) == 0: return orderstr[num//100-1]+'百'
if (num%100) < 10: return orderstr[num//100-1] + "百零" + orderstr[(num%10)-1]
num3 = num//100
num2 = (num%100)//10
num1 = num%10
if num1 > 0: return '%s百%s十%s'%(orderstr[num3-1], orderstr[num2-1], orderstr[num1-1])
else: return '%s百%s十'%(orderstr[num3-1], orderstr[num2-1])
#------------------------------------------------------------
insertall = ""
for id,t in enumerate(strall):
numstr = chinesestr99(id+1)
ts = "## <font face=黑体 color=purple>%s、%s</font>\\n\\n\\n"%(numstr, t)
insertall = insertall + ts
printf(insertall)
clipboard.copy(insertall)
tspsendwindowkey(csdn_window, "v", control=1)
#------------------------------------------------------------
# END OF FILE : TEST1.PY
#============================================================
以上是关于自己珍藏一些有趣的Python子程序的主要内容,如果未能解决你的问题,请参考以下文章