python笔记-02
-
列表里删除用户
remove:删除列表里面的指定元素
pop:删除列表里面指定位置的元素,默认是最后一位
-
break 退出死循环;exit(0)退出整个程序;sys.exit(0)
-
存储数据到字典可以这样写
t[teacher_name]=passwd
-
加密
-
import hashlib def encry_password(passwd): h=hashlib.sha256() h.update(passwd.encode('utf8')) return h.hexdigest()
-
读json数据
-
def read_json(filename,default_data): try: with open(BASE_DIR+filename,'r',encoding='utf8') as file: import json return json.load(file) except FileNotFoundError: return default_data
-
对象转换为字典,再存储到列表中
-
stu = model.Student(stu_name,stu_age,stu_gender,stu_tel) students=[] students.append(stu.__dict__) data={'all_student':students}
-
过滤器遍历字典传值
same_id_students = list(filter(lambda s:s['s_id'] == s_id,students)) if not same_id_students: print('未找到这个学生') else: for s in same_id_students: print('学号{s_id},姓名{name},年龄{age},性别{gender},电话{tel}'.format(**s))
-
简化方法
-
operator = input('1.按姓名删除\n2.按学号删除\n3.返回\n') if operator == '1': key = 'name' value = input('请输入姓名:') elif operator == '2': key = 'name' value = input('请输入学号:') elif operator == '3': return students = list(filter(lambda s:s.get(key,'') == value,all_students))
-
对列表中的字典进行排序
students.sort(key=lambda x:x['python'],reverse = False) #[{"id": "0001", "name": "miao", "python": "10", "java": "90", "c": "100"}, {"id": "0002", "name": "cheng", "python": "99", "java": "99", "c": "99"}]
-
Python里面一切都是对象,list的复制需要用[:]的方式
a = [1, 2, 3] b = a[:] a[0]=0 print(id(a),id(b)) # 可以看到a,b的内存不一致,是复制 print(a,b) # 修改a,b不变
-
time.sleep(1),用过的都知道。 暂停一秒
import time a = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) # time.localtime()时间戳转化成时间元祖 print(a) time.sleep(1) b = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) # time.strftime()时间元祖转化成时间文本 print(b)
-
判断一个文件夹是否存在,没有就自动创建
def mkdir(path): isexists = os.path.exists(path) if not isexists: os.mkdir(path)
-
通过qrcode生成二维码
import qrcode img = qrcode.make('http://lizhiyu.iteye.com/blog/2331662') img.save('test.png') #加参数 import qrcode qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) qr.add_data('http://lizhiyu.iteye.com/') qr.make(fit=True) img = qr.make_image() img.save('123.png') version:值为1~40的整数,控制二维码的大小(最小值是1,是个12×12的矩阵)。 如果想让程序自动确定,将值设置为 None 并使用 fit 参数即可。 error_correction:控制二维码的错误纠正功能。可取值下列4个常量。 ERROR_CORRECT_L:大约7%或更少的错误能被纠正。 ERROR_CORRECT_M(默认):大约15%或更少的错误能被纠正。 ROR_CORRECT_H:大约30%或更少的错误能被纠正。 box_size:控制二维码中每个小格子包含的像素数。 border:控制边框(二维码与图片边界的距离)包含的格子数(默认为4,是相关标准规定的最小值)
-
随机抽取三个位置
randfir = random.sample(number, 3) # 随机抽取防伪码中的三个位置,不分先后 randsec = sorted(randfir) # 对抽取的位置进行排序并存储给randsec变量,以便按顺序排列三个字母的位置 int(randsec[0])#类似这样取数
-
计算条形码
barcode=mainid+compid+strone evensum=int(barcode[1])+int(barcode[3])+int(barcode[5])+int(barcode[7])+int(barcode[9])+int(barcode[11]) oddsum=int(barcode[0])++int(barcode[2])+int(barcode[4])+int(barcode[6])+int(barcode[8])+int(barcode[10]) checkbit=int((10-(evensum*3-oddsum)%10)%10) barcode=barcode+str(checkbit) encoder=EAN13Encoder(barcode) encoder.save("barcode\\"+barcode+".png")
-
生成二维数组
checkboard=[] for i in range(10): checkboard.append([]) for j in range(10): checkboard[i].append('-')