目录
题记
风雨送春归,飞雪迎春到。眼下虽还未逢春,但依然值得我们好好总结一番,以饱满的精神面貌面对2023年。
我的碎碎念
回首2022年一年的收获,我不禁慨叹,知识的大洋是那样迷人,是那样浩渺无边。
作为一个信息技术初学者,我在这一年里,重新接触并初步了解了python语言,学习了部分技术常识和算法知识。
我回想了一下,这一年里,我系统学习了:
1)int、float、str、boolean等基本数据类型
2)list、dict、tuple、array等复合数据类型
3)pandas.Series、pandas.DataFrame、io.text等常用数据类型
4)初步了解pandas、numpy、openpyxl、math等常用库
5)使用turtle、random、sys、os、time等基础常用库
6)使用pyperclip等库
7)函数基础构造方法、调用方法
除此之外,还进行了数十个个简单实例编程,写了几篇文章。
现在想想,其实一年时间没干什么,却又充实得很。
唉~
没话说了,放几个实例吧。
简单python实例
绘画类
谁不喜欢画画呢?用或简单或复杂的代码带动小海龟,爬过那些疲倦的时候。
(1)draw.py
简单而无脑的turtle画板,发呆时的首选
![](https://img-blog.csdnimg.cn/7ec9a460aaa94c4faaab39bf533c32ac.png)
import turtle as t
def wrd():
t.write(t.pos(),move=False,align='left',font=('Arial',15,'normal'))
def setpos(x,y):
t.goto(x,y)
wrd()
def up_ward():
t.setheading(90)
t.forward(15)
def down_ward():
t.setheading(270)
t.forward(15)
def left_ward():
t.setheading(180)
t.forward(15)
def right_ward():
t.setheading(0)
t.forward(15)
def head_left():
t.setheading(180)
def head_right():
t.setheading(0)
def speed0():
t.speed(0)
def speed1():
t.speed(1)
def speed5():
t.speed(5)
def speed10():
t.speed(10)
def gr():
t.pencolor('Palegreen1')
def bl():
t.pencolor('Black')
def wt():
t.pencolor('white')
t.pd()
t.color('Palegreen1','purple')
t.pensize(10)
t.bgcolor('black')
t.listen()
t.onscreenclick(setpos)
t.onkeypress(t.clear,'c')
t.onkeypress(t.pu,'Up')
t.onkeypress(t.pd,'Down')
t.onkeypress(up_ward,'w')
t.onkeypress(down_ward,'s')
t.onkeypress(left_ward,'a')
t.onkeypress(right_ward,'d')
t.onkeypress(head_left,'Left')
t.onkeypress(head_right,'Right')
t.onkeypress(speed0,'1')
t.onkeypress(speed1,'2')
t.onkeypress(speed5,'3')
t.onkeypress(speed10,'4')
t.onkeypress(gr,'g')
t.onkeypress(bl,'b')
t.onkeypress(wt,'t')
(2)creeper_painter.py
经典像素风,你听到“嘶嘶”的声音了吗?
import turtle as t
def cr_help():
count=0
b=5
print('''This function contains 6 sub-functions.
Include:
creeper_paleside
creeper_head_cl
creeper_head_total
cr_help
wrd
turtle
''')
while count<=b:
count+=1
_a=input('按下回车键以继续...(输入任意函数名以查看对应帮助;输入任意数以退出)')
if _a=='' and count==1 or _a=='creeper_paleside':
print('''------creeper_paleside------
This function will paint a paleside of the creeper.
Creeper has a structure of 8*8 chart.
It will be shown in this function.''')
elif _a=='' and count==2 or _a=='creeper_head_cl':
print('''------creeper_head_cl------
This function support a classical painting mode.
This is the main function in this creeper_painter.
It will paint a creeper with full colours.
Try it!''')
elif _a=='' and count==3 or _a=='creeper_head_total':
print('''------creeper_head_total------
This function provide a combine effect.
It mix two main functions together.
You can have it done a perfect work.''')
elif _a=='' and count==4 or _a=='wrd':
print('''------wrd------
This is a normal function.
You can use it with a for--range while.
It is for writing words in turtle.
The attribute 'drawingspeed' in function 'creeper_paleside' is the same as the one in turtle.speed()''')
elif _a=='' and count==5 or _a=='turtle':
print('''------turtle------
This is a function imported from Python Libs.
It is the popular 2-D Graphics.''')
elif _a=='cr_help':
print('''Wow! You are so bored!
This is only the help function for creeper_painter.
You are using it now!
Noting else.''')
else:
print('quit')
break
def wrd(target_word, startx, starty):
t.color("black", "black")
t.pu()
t.goto(startx,starty)
t.pd()
t.write(target_word, move=False,align='left',font=('Arial',35,'normal'))
def creeper_paleside(drawingspeed):
#设置速度变量
t.speed(drawingspeed)
#竖条
t.pu()
t.goto(-200,200)
t.seth(90)
t.pd()
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
#边缘
t.right(90)
t.forward(400)
t.right(90)
t.forward(400)
t.right(90)
t.forward(400)
t.right(90)
t.forward(400)
#横条
t.right(180)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
def creeper_head_cl():
t.speed(0)
list1=['DarkSeaGreen1','Green3','Chartreuse3','DarkSeaGreen','Snow2','PaleGreen','PaleGreen3','DarkGreen']
list2=['ForestGreen','PaleGreen','LimeGreen','PaleGreen','PaleGreen4','LawnGreen','YellowGreen','Seashell3']
list3=['Green','DarkGreen','LemonChiffon4','PaleGreen2','DarkSeaGreen3','DarkGreen','LemonChiffon4','LightYellow2']
list4=['Green1','DarkGreen','Black','LawnGreen','Green1','Black','DarkGreen','PaleGreen']
list5=['PaleGreen1','DarkOliveGreen3','PaleGreen','DarkGreen','DarkGreen','LightGray','PaleGreen4','Green1']
list6=['PaleGreen1','DarkSeaGreen3','LemonChiffon4','Black','Black','ForestGreen','LawnGreen','Seashell3']
list7=['Ivory1','DarkSeaGreen3','Black','Black','Black','Black','PaleGreen','LawnGreen']
list8=['LimeGreen','PaleGreen','DarkGreen','DarkSeaGreen','PaleGreen','Chartreuse4','LawnGreen','PaleGreen']
t.pu()
t.goto(-200,200)
t.seth(0)
#第1行
t.bgcolor('LightGoldenrod1')
for i in list1:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,150)
#第2行
t.bgcolor('MediumAquamarine')
for i in list2:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,100)
#第3行
t.bgcolor('MidnightBlue')
for i in list3:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,50)
#第4行
t.bgcolor('Chocolate')
for i in list4:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,0)
#第5行
t.bgcolor('Firebrick2')
for i in list5:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,-50)
#第6行
t.bgcolor('VioletRed')
for i in list6:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,-100)
#第7行
t.bgcolor('Thistle')
for i in list7:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,-150)
#第8行
t.bgcolor('Plum')
for i in list8:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu()
#写字
l=['C','R','E','E','P','E','R']
for i in range(0,7):
wrd(l[i],-100+i*28,-250)
t.exitonclick()
def creeper_head_total():
#paleside
#竖条
t.speed(5)
t.pu()
t.goto(-200,200)
t.pd()
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
#边缘
t.right(90)
t.forward(400)
t.right(90)
t.forward(400)
t.right(90)
t.forward(400)
t.right(90)
t.forward(400)
#横条
t.right(180)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(400)
t.right(90)
t.forward(50)
t.right(90)
t.forward(400)
t.left(90)
#fullhead
t.speed(0)
list1=['DarkSeaGreen1','Green3','Chartreuse3','DarkSeaGreen','Snow2','PaleGreen','PaleGreen3','DarkGreen']
list2=['ForestGreen','PaleGreen','LimeGreen','PaleGreen','PaleGreen4','LawnGreen','YellowGreen','Seashell3']
list3=['Green','DarkGreen','LemonChiffon4','PaleGreen2','DarkSeaGreen3','DarkGreen','LemonChiffon4','LightYellow2']
list4=['Green1','DarkGreen','Black','LawnGreen','Green1','Black','DarkGreen','PaleGreen']
list5=['PaleGreen1','DarkOliveGreen3','PaleGreen','DarkGreen','DarkGreen','LightGray','PaleGreen4','Green1']
list6=['PaleGreen1','DarkSeaGreen3','LemonChiffon4','Black','Black','ForestGreen','LawnGreen','Seashell3']
list7=['Ivory1','DarkSeaGreen3','Black','Black','Black','Black','PaleGreen','LawnGreen']
list8=['LimeGreen','PaleGreen','DarkGreen','DarkSeaGreen','PaleGreen','Chartreuse4','LawnGreen','PaleGreen']
t.pu()
t.goto(-200,200)
t.seth(0)
#第1行
t.bgcolor('LightGoldenrod1')
for i in list1:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,150)
#第2行
t.bgcolor('MediumAquamarine')
for i in list2:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,100)
#第3行
t.bgcolor('MidnightBlue')
for i in list3:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,50)
#第4行
t.bgcolor('Chocolate')
for i in list4:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,0)
#第5行
t.bgcolor('Firebrick2')
for i in list5:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,-50)
#第6行
t.bgcolor('VioletRed')
for i in list6:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,-100)
#第7行
t.bgcolor('Thistle')
for i in list7:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu
t.goto(-200,-150)
#第8行
t.bgcolor('Plum')
for i in list8:
t.pd()
t.pencolor(i)
t.fillcolor(i)
t.begin_fill()
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
t.pu()
t.forward(50)
t.pu()
#写字
l=['C','R','E','E','P','E','R']
for i in range(0,7):
wrd(l[i],-100+i*28,-250)
t.exitonclick()
(3)triangle.py
这真的是三角形,只不过量多了一点
import turtle as t
from random import randint as r
import time as tt
def tri():
t.pd()
l=5
t.speed('fastest')
t.colormode(255)
for time in range(10):
for i in range(10):
for a in range(3):
t.right(120)
t.color((r(0,255),r(0,255),r(0,255)))
t.forward(l)
t.pu()
t.forward(l)
t.pd()
t.color((r(0,255),r(0,255),r(0,255)))
t.forward(l)
t.right(36)
l=l+5
t.pu()
t.goto(500,-200)
if __name__ == '__main__':
tri()
文件编辑类
(1)finder.py
emmm...
某种程度上,访达这个译名和它确实挺搭。
import os
def mode1():
bad_f_list=['']
src_f_list=['0']
tgt_wd_list=['0']
tgt_f_list=[]
while src_f_list[-1]!='':
src_f_list.append(input('请键入文件完全名称>>['))
src_f_list.pop(0)
src_f_list.pop(-1)
while tgt_wd_list[-1]!='':
tgt_wd_list.append(input('请键入关键词>>['))
tgt_wd_list.pop(0)
tgt_wd_list.pop(-1)
for src_f in src_f_list:
try:
try:
with open(src_f,'r') as f:
try:
src_wd=f.read()
for tgt_wd in tgt_wd_list:
if tgt_wd in src_wd:
tgt_f_list.append(src_f)
except UnicodeDecodeError:
bad_f_list.append(src_f)
except IsADirectoryError:
bad_f_list.append(src_f)
except FileNotFoundError:
bad_f_list.append(src_f)
if bad_f_list!=['']:
bad_f_list.pop(0)
print('搜索到包含关键词的文件为:',tgt_f_list,'您输入的文件中包含无效文件:',bad_f_list,sep='\n')
else:
print('搜索到包含关键词的文件为:',tgt_f_list)
def mode2():
t_bad_f_list=['']
c_bad_f_list=['']
bad_fd_list=['']
t_src_f_list=[]
c_src_f_list=[]
src_fd_list=['0']
tgt_wd_list=['0']
t_tgt_f_list=[]
c_tgt_f_list=[]
while src_fd_list[-1]!='':
src_fd_list.append(input('请键入文件夹地址>>['))
src_fd_list.pop(0)
src_fd_list.pop(-1)
while tgt_wd_list[-1]!='':
tgt_wd_list.append(input('请键入关键词>>['))
tgt_wd_list.pop(0)
tgt_wd_list.pop(-1)
cc=0
c=0
try:
for src_fd in src_fd_list:
c_bad_f_list=['']
c_tgt_f_list=[]
c_src_f_list=os.listdir(src_fd)
try:
for f in c_src_f_list:
if '.'==list(f)[0]:
c_bad_f_list.append(f)
cc=1
if cc==1:
c_bad_f_list.pop(0)
for f in c_bad_f_list:
c_src_f_list.remove(f)
for f in c_src_f_list:
for l in list(f):
if l=='.':
c=1
if c!=1:
c_bad_f_list.append(f)
if cc!=1 and c!=1:
c_bad_f_list.pop(0)
for f in c_bad_f_list:
try:
c_src_f_list.remove(f)
except ValueError:
n=0
for src_f in c_src_f_list:
try:
with open(src_fd+'/'+src_f,'r') as f:
for tgt_wd in tgt_wd_list:
try:
src_wd=f.read()
if tgt_wd in src_wd:
c_tgt_f_list.append(src_f)
except UnicodeDecodeError:
c_bad_f_list.append(src_f)
except IsADirectoryError:
c_bad_f_list.append(src_f)
if cc!=1 and c==1 and c_bad_f_list!=['']:
c_bad_f_list.pop(0)
for f in c_bad_f_list:
try:
c_src_f_list.remove(f)
except ValueError:
n=0
for f in c_tgt_f_list:
c_src_f_list.remove(f)
except IsADirectoryError:
c_bad_f_list.append(src_f)
for value in range(len(c_src_f_list)):
t_src_f_list.append(src_fd+'/'+c_src_f_list[value])
if c_bad_f_list!=['']:
for value in range(len(c_bad_f_list)):
t_bad_f_list.append(src_fd+'/'+c_bad_f_list[value])
for value in range(len(c_tgt_f_list)):
t_tgt_f_list.append(src_fd+'/'+c_tgt_f_list[value])
except FileNotFoundError:
bad_fd_list.append(src_fd)
if t_bad_f_list!=[''] and bad_fd_list!=['']:
t_bad_f_list.pop(0)
bad_fd_list.pop(0)
print('搜索到包含关键词的文件为:',t_tgt_f_list,'您输入的文件中包含无效文件:',t_bad_f_list,'您输入的文件夹中包含无效文件夹:',bad_fd_list,'您输入的文件中包含未搜索到关键词的有效文件:',t_src_f_list,sep='\n')
elif t_bad_f_list!=['']:
t_bad_f_list.pop(0)
print('搜索到包含关键词的文件为:',t_tgt_f_list,'您输入的文件中包含无效文件:',t_bad_f_list,'您输入的文件中包含未搜索到关键词的有效文件:',t_src_f_list,sep='\n')
elif bad_fd_list!=['']:
bad_fd_list.pop(0)
print('搜索到包含关键词的文件为:',t_tgt_f_list,'您输入的文件夹中包含无效文件夹:',bad_fd_list,'您输入的文件中包含未搜索到关键词的有效文件:',t_src_f_list,sep='\n')
else:
print('搜索到包含关键词的文件为:',t_tgt_f_list,'您输入的文件中包含未搜索到关键词的有效文件:',t_src_f_list,sep='\n')
print('''本文件可以帮助您搜索具有特定信息的文本文档。
本文件支持多关键词同时搜索。''')
mode=input('''
本文件共有两个模式:
mode1.分散文件搜索
mode2.目录下文件搜索
请选择需要的模式>>[''')
if '1' in mode:
mode1()
if '2' in mode:
mode2()
#update time>>[2022/8/26 22:41
#文本解析系列_寻找字符串
(2)indenter.py
缩进可是python一大特色,在倒腾函数时,突然想要把它扔在类对象下,又嫌麻烦怎么办?
当然是自动化添加缩进...
def indenter(word:str, indentation_add_num):
num = indentation_add_num
indents = ' ' * num
result = ''
word = word.strip().split('\n')
word = [indents + i + '\n' for i in word]
for line in word:
result += line
return word, result
def indenter_clip(word:str, indentation_add_num):
import pyperclip as pp
num = indentation_add_num
indents = ' ' * num
result = ''
word = word.strip().split('\n')
word = [indents + i + '\n' for i in word]
for line in word:
result += line
pp.copy(result)
print(f'''Indentation (num:{num}) successfully added.
缩进(数量:{num})添加成功。''')
return word, result
#缩进添加器 V1.0
#Date: 31 Dec, 2022
(3)book.py
这是一个很有趣但一定程度上特化的程序。
快速创建不同章节的文档。
import os
import time as t
def mode_all_same():#OK
global file_list_1
chapter=int(input("What's the last chapter? >>[").strip())+1
section_num=int(input('How many sections are there in each chapter? >>[').strip())+1
section_list=[]
file_list_1=[]
chapter_list=[]
Error_count=0
for i in range(section_num):
section_list.append(i)
section_list.pop(0)
for i in range(chapter):
chapter_list.append(i)
chapter_list.pop(0)
for chapter in chapter_list:
for section in section_list:
path=f'{directory}/{key_wd}/{key_wd}_{chapter}/{key_wd}_{chapter}_{section}{format_}'
try:
try:
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_1.append(path)
except FileNotFoundError:
try:
directory_=f'{directory}/{key_wd}/{key_wd}_{chapter}'
os.mkdir(directory_)
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_1.append(path)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
except FileNotFoundError:#上级目录f'{directory}/{key_wd}'不存在,同时raise两个FileNotFoundError。
directory_=f'{directory}/{key_wd}'
os.mkdir(directory_)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
directory_=f'{directory}/{key_wd}/{key_wd}_{chapter}'
os.mkdir(directory_)
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_1.append(path)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
except FileExistsError:
Error_count+=1
print(f'Error[{Error_count}]:File already exists. NameCode:{chapter}.{section}')
file_list_1.append(path)
print('\nYour creat command has already done.','Here is the file list:',sep='\n')
for file in file_list_1:
print(f'\t{file}')
def mode_choose_same():#OK
global file_list_2
same_ch_list=[0]
dif_ch_list=[0]
dif_sec_list=[0]
section_same_list=[]
file_list_2=[]
Error_count=0
while same_ch_list[-1]!='!':
same_ch_list.append(input('Please key in the same chapters one by one\n(only takes the most)(put a "!" in list when all in place). >>[').strip())
section_same_num=int(input('How many sections are there in each then? >>[').strip())+1
print('Please key in other chapters and their section numbers one by one\n(only takes one integer at a time)(put a "!" in list twice when all in place).')
while dif_sec_list[-1]!='!':
dif_ch_list.append(input('chapter >>[').strip())
#print('chapters:',dif_ch_list)
dif_sec_list.append(input('section(s) >>[').strip())
#print('sections:',dif_sec_list)
same_ch_list.pop(0);same_ch_list.pop(-1)
dif_ch_list.pop(0);dif_ch_list.pop(-1)
dif_sec_list.pop(0);dif_sec_list.pop(-1)
dif_list=list(zip(dif_ch_list,dif_sec_list))
#print('dif::',dif_list)
for i in range(section_same_num):
section_same_list.append(i)
section_same_list.pop(0)
for chapter in same_ch_list:#same part
for section in section_same_list:
path=f'{directory}/{key_wd}/{key_wd}_{chapter}/{key_wd}_{chapter}_{section}{format_}'
try:
try:
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_2.append(path)
except FileNotFoundError:
try:
directory_=f'{directory}/{key_wd}/{key_wd}_{chapter}'
os.mkdir(directory_)
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_2.append(path)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
except FileNotFoundError:#上级目录f'{directory}/{key_wd}'不存在,同时raise两个FileNotFoundError。
directory_=f'{directory}/{key_wd}'
os.mkdir(directory_)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
directory_=f'{directory}/{key_wd}/{key_wd}_{chapter}'
os.mkdir(directory_)
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_2.append(path)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
except FileExistsError:
Error_count+=1
print(f'Error[{Error_count}]:File already exists. NameCode:{chapter}.{section}')
file_list_2.append(path)
for (chapter,section_num) in dif_list:#different part
#print('enter dif.cycle','chapter is',chapter,'section_num is',section_num)
section_dif_list=[]
for i in range(int(section_num)+1):
section_dif_list.append(i)
section_dif_list.pop(0)
#print('section_dif_list done:',section_dif_list)
for section in section_dif_list:
path=f'{directory}/{key_wd}/{key_wd}_{chapter}/{key_wd}_{chapter}_{section}{format_}'
#print('path done',path)
try:
try:
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_2.append(path)
except FileNotFoundError:
try:
directory_=f'{directory}/{key_wd}/{key_wd}_{chapter}'
os.mkdir(directory_)
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_2.append(path)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
except FileNotFoundError:#上级目录f'{directory}/{key_wd}'不存在,同时raise两个FileNotFoundError。
directory_=f'{directory}/{key_wd}'
os.mkdir(directory_)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
directory_=f'{directory}/{key_wd}/{key_wd}_{chapter}'
os.mkdir(directory_)
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_2.append(path)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
except FileExistsError:
Error_count+=1
print(f'Error[{Error_count}]:File already exists. NameCode:{chapter}.{section}')
file_list_2.append(path)
print('\nYour creat command has already done.','Here is the file list:',sep='\n')
file_list_2.sort()
for file in file_list_2:
print(f'\t{file}')
def mode_self_choose():#OK
global file_list_3
dif_ch_list=[0]
dif_sec_list=[0]
file_list_3=[]
Error_count=0
print('Please key in the chapters and their section numbers one by one\n(only takes one integer at a time)(put a "!" in list twice when all in place).')
while dif_sec_list[-1]!='!':
dif_ch_list.append(input('chapter >>[').strip())
dif_sec_list.append(input('section(s) >>[').strip())
dif_ch_list.pop(0);dif_ch_list.pop(-1)
dif_sec_list.pop(0);dif_sec_list.pop(-1)
dif_list=list(zip(dif_ch_list,dif_sec_list))
for (chapter,section_num) in dif_list:
section_dif_list=[]
for i in range(int(section_num)+1):
section_dif_list.append(i)
section_dif_list.pop(0)
for section in section_dif_list:
path=f'{directory}/{key_wd}/{key_wd}_{chapter}/{key_wd}_{chapter}_{section}{format_}'
try:
try:
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_3.append(path)
except FileNotFoundError:
try:
directory_=f'{directory}/{key_wd}/{key_wd}_{chapter}'
os.mkdir(directory_)
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_3.append(path)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
except FileNotFoundError:#上级目录f'{directory}/{key_wd}'不存在,同时raise两个FileNotFoundError。
directory_=f'{directory}/{key_wd}'
os.mkdir(directory_)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
directory_=f'{directory}/{key_wd}/{key_wd}_{chapter}'
os.mkdir(directory_)
with open(path,'x') as f:
f.write(f'#{key_wd} {chapter}.{section}')
f.close()
file_list_3.append(path)
Error_count+=1
print(f"Error[{Error_count}]:\nThe directory:'{directory_}' used not to exist.")
except FileExistsError:
Error_count+=1
print(f'Error[{Error_count}]:File already exists. NameCode:{chapter}.{section}')
file_list_3.append(path)
print('\nYour creat command has already done.','Here is the file list:',sep='\n')
file_list_3.sort()
for file in file_list_3:
print(f'\t{file}')
def write_mode_1():
dealing=[]
result='\nimport os\nimport time as t\n'
count=0
with open('book.py','r') as f:
lines=f.readlines()
for l in lines:
dealing.append(l)
count+=1
if l=='def mode_all_same():#OK\n':
low=count-1
if l=='def mode_choose_same():#OK\n':
high=count-1
for i in range(low,high):
result+=dealing[i]
count=0
for l in lines:
count+=1
if l=='key_wd=input("What\'s your key word? >>[")\n':
low=count-1
for i in range(low,low+3):#meet an unkown Error: can't match low_key and l
result+=dealing[i]
result+='mode_all_same()\n\n#Here is the file list last time.\n#registered time see header\n#List:'
for file in file_list_1:
result+=f'\n#\t{file}'
return result
def write_mode_2():
dealing=[]
result='\nimport os\nimport time as t\n'
count=0
with open('book.py','r') as f:
lines=f.readlines()
for l in lines:
dealing.append(l)
count+=1
if l=='def mode_choose_same():#OK\n':
low=count-1
if l=='def mode_self_choose():#OK\n':
high=count-1
for i in range(low,high):
result+=dealing[i]
count=0
for l in lines:
count+=1
if l=='key_wd=input("What\'s your key word? >>[")\n':
low=count-1
for i in range(low,low+3):#meet an unkown Error: can't match low_key and l
result+=dealing[i]
result+='mode_choose_same()\n\n#Here is the file list last time.\n#registered time see header\n#List:'
for file in file_list_2:
result+=f'\n#\t{file}'
return result
def write_mode_3():
dealing=[]
result='\nimport os\nimport time as t\n'
count=0
with open('book.py','r') as f:
lines=f.readlines()
for l in lines:
dealing.append(l)
count+=1
if l=='def mode_self_choose():#OK\n':
low=count-1
if l=='def write_mode_1():\n':
high=count-1
for i in range(low,high):
result+=dealing[i]
count=0
for l in lines:
count+=1
if l=='key_wd=input("What\'s your key word? >>[")\n':
low=count-1
for i in range(low,low+3):#meet an unkown Error: can't match low_key and l
result+=dealing[i]
result+='mode_self_choose()\n\n#Here is the file list last time.\n#registered time see header\n#List:'
for file in file_list_3:
result+=f'\n#\t{file}'
return result
key_wd=input("What's your key word? >>[")
format_=input('What format do you need for your files?\n(plain text format)(e.g.:.txt)>>[')
directory=input('Which directory do you choose to save your work?\n(need an abs-path)>>[')
choice=input('Do the chapters which had the same number of sections reach half of the total?\n(T/F or 1/0)>>[')
if 'T' in choice or '1' in choice:
choice=input('Does each chapter have same number of sections? (T/F or 1/0)>>[').strip()
if 'T' in choice or '1' in choice:
mode_all_same()
mode=1
else:
mode_choose_same()
mode=2
else:
mode_self_choose()
mode=3
choice=input("Do you want to remain the settings this time?\nI'll make you a new Python programme.\n(T/F or 1/0)>>[")
if 'T' in choice or '1' in choice:
year,mon,day,hour,min_,s,wday,yday,isdst=t.localtime()
mon_dict={'1':'Jan','2':'Feb','3':'Mar','4':'Apr','5':'May','6':'Jun',
'7':'Jul','8':'Aug','9':'Sept','10':'Oct','11':'Nov','12':'Dec'}
try:
with open(f'{directory}/{key_wd}/writer.py','x') as f:
f.write(f'#Remain the settings on {day} {mon_dict[str(mon)]},{year}(at{hour}:{min_})\n#Please use this file for the same settings next time.')
if mode==1:
f.write(write_mode_1())
elif mode==2:
f.write(write_mode_2())
elif mode==3:
f.write(write_mode_3())
else:
f.close()
f.close()
filename=f'{directory}/{key_wd}/writer.py'
except FileExistsError:
with open(f'{directory}/{key_wd}/writer_{hour}_{min_}_{s}.py','x') as f:
f.write(f'#Remain the settings on {day} {mon_dict[str(mon)]},{year}(at{hour}:{min_})\n#Please use this file for the same settings next time.')
if mode==1:
f.write(write_mode_1())
elif mode==2:
f.write(write_mode_2())
elif mode==3:
f.write(write_mode_3())
else:
f.close()
f.close()
filename=f'{directory}/{key_wd}/writer_{hour}_{min_}_{s}.py'
if hour>20:
if min_>=30:
print(f'The settings have been remained in file:\n{filename}\nThanks for using!\nWish you a good night!')
else:
print(f'The settings have been remained in file:\n{filename}\nThanks for using!\nWish you a good day!')
else:
print(f'The settings have been remained in file:\n{filename}\nThanks for using!\nWish you a good day!')
else:
print('Good Bye!')
#chapter§ions files creater
#This is the FATHER edition in English to all affiliated files.
#accomplished time: 26 Jul, 2022 (12:12 at Noon)
#inspired by exer_file_creater.py
其他类
(1)AreYouReady.py
疫情当下,学生们纷纷开启网课学习。点名报到成为问题。
from bs4 import BeautifulSoup as bs
import requests as r
import pyperclip as p
class main_:
def _init_():
members_tgt_dct = {
1:'安梓铭',2:'陈宇曦',3:'丁凤堃',4:'冯宜宁',5:'高梓君',6:'龚依恒',
7:'顾惟涵',8:'管星宇',9:'李瑾萱',10:'李路凯',12:'唐楚辰',
13:'万禹迪',14:'王思然',15:'王星皓',16:'王梓昊',17:'吴祈瑞',18:'夏承扬',
19:'徐峥硕',20:'许成安',21:'杨晓悦',22:'杨宗坤',23:'臧宇赫',24:'张涵琪',
25:'张檀玉',26:'张雪嫣',27:'赵文轩',28:'赵羽岚',29:'赵志远',30:'周孝勖'}
url = ''
return members_tgt_dct, url
def check(members_tgt_dct, members_src_lst, members_ready = [],
members_absent = [], con_code = 0, members_ready_collect = []):
members_tgt_lst = list(members_tgt_dct.values())
for mem_tgt in members_tgt_lst:
con_code = 0
for mem_src in members_src_lst:
if mem_tgt in mem_src:
members_ready.append(mem_tgt)
members_ready_collect.append(mem_src)
con_code = 1
if con_code == 0:
members_absent.append(mem_tgt)
for mem in members_ready_collect:
members_src_lst.remove(mem)
others = members_src_lst[:]
copying = '以下同学未到:\n'
for ab in members_absent:
copying += ab + '\n'
p.copy(copying)
result = f'''应到{len(members_tgt_lst)}人,实到{len(members_ready)}人,未到{len(members_absent)}人。
\n实际出勤名单如下:\n{members_ready}\n\n未到人员名单如下:\n{members_absent}\n\n输入名单中其他人员有:\n{others}\n
注:未到人员名单已经拷贝到剪贴板。\n'''
return members_ready, members_absent, result, members_tgt_lst, others
def web_catch(url):
pass
def _inter_man_():
members_tgt_dct, url = main_._init_()
print('欢迎使用 网课出勤检查工具 V1.0 ,目前录入的人员名单是尚雅班名单。')
members_src_lst = input('请输入已到人员名单(每输入一个,空一格):').strip().split(' ')
members_ready, members_absent, result ,members_tgt_lst, others = main_.check(members_tgt_dct, members_src_lst)
print(result)
return members_ready, members_absent, members_tgt_lst
def _inter_semiauto_():
members_tgt_dct, url = main_._init_()
main_.write_in()
members_src_lst = main_.get_members_lst()
members_ready, members_absent, result ,members_tgt_lst, others = main_.check(members_tgt_dct, members_src_lst)
print(result)
return members_ready, members_absent, members_tgt_lst
def get_members_lst():
with open('/Applications/coding/ Folder Python/classwork/members_list.txt','r') as f:
members_src_lst = f.readlines()
f.close()
#members_src_lst = members_src_lst.replace('\n',' ',members_src_lst.count('\n'))
return members_src_lst
def write_in():
print('请在“开始录入”提示语显示时开始复制要录入的内容,并单独录入“STOPSTOP”作为录入停止符。')
word = ''
words = []
print('\n请注意:\n\n\t\t\t\t 开始录入\n')
while 'STOPSTOP' not in word:
word = p.waitForNewPaste(timeout=120)
words.append(word)
print('录入成功。\n\n\n')
words.pop(-1)
with open('/Applications/coding/ Folder Python/classwork/members_list.txt','w') as f:
for word in words:
f.write(word)
f.close()
if __name__ == '__main__':
choice = input('请选择模式:\n(1)手动输入\n(2)半自动模式\n(3)全自动模式[暂缓开通]\n>>[')
if '1' in choice:
main_._inter_man_()
elif '2' in choice:
main_._inter_semiauto_()
else:
print('抱歉,该功能正在开发中。')
#网课出勤检查工具 V2.0
#Date:26 Dec, 2022
#Versions:
# V1.0 Created with main functions of check module and standard initalization module.
# V2.0 Add new modules{_inter_man_, get_members_lst, _inter_semiauto_}
# V2.5 Add new function: receive a list of absent students in clipboard.
实例太多啦,暂且介绍这几个。
几句话
俗话说“人生苦短,我用python”,可“自其不变者而观之,则物与我皆无尽也,而又何羡乎”。我的人生刚刚起步,我很庆幸身处这样一个和平富强、持续发展的祖国,我也很感谢教育者们、先辈们的帮助。
遇见CSDN是年初的事了。这好似让我拥怀了茫茫知海里一个明亮的精神坐标。
我相信在往后的日子里一定有不少苦头要吃,但生活不就是这样吗?很多时候,人要在精神面貌上理想一点,在实际行动中现实一点。
希望新的一年里,诸事顺遂,平平稳稳。