random、time、os、json、pickle模块
#1.random模块
import random #常规代码 def func(n=6,m=True):#随机生成4到6位数字或者字母+数字验证码 code = "" if m: for i in range(n): num = str(random.randint(0, 9)) upper = chr(random.randint(65, 90)) lower = chr(random.randint(97, 122)) code += random.choice([num, upper, lower]) else: for i in range(n): code += str(random.randint(0, 9)) return code #进阶代码 def func1 (n=6,m=True):#随机生成4到6位数字或者字母+数字验证码 code="" for i in range(n): num=str(random.randint(0,9)) if m: upper=chr(random.randint(65,90)) lower=chr(random.randint(97,122)) num=random.choice([num,upper,lower]) code+=num return code #调用函数 print(func1()) print(func1(4,False))
#2.time模块
import time
#时间戳—》结构化时间 print(time.localtime())#time.localtime()中不填值默认为是当前时间 print(time.gmtime())#time.gmtime()是显示伦敦时间与localtime()差8小时 #结构化时间-》字符串时间 print(time.strftime("%Y-%m-%d %X")) print(time.strftime("%Y-%m-%d %X",time.localtime())) #字符串时间-》结构化时间 print(time.strptime("2018-8-20","%Y-%m-%d")) #结构化时间-》时间戳 print(time.mktime(time.localtime()))#时间戳-》字符串时间 print(time.ctime(15000000)) print(time.ctime())#time.ctime()中不填值默认为是当前时间 #结构化时间-》字符串时间 print(time.asctime(time.localtime())) print(time.asctime())#time.asctime()中不填值默认为是当前时间
# 1.查看一下2000000000时间戳时间表示的年月日
# 2.将2008-8-8转换成时间戳时间
# 3.请将当前时间的当前月1号的时间戳时间取出来 - 函数
# 4.计算时间差
# 2018-8-19 22:10:8 2018-8-20 11:07:3
# 经过了多少时分秒
# 2.将2008-8-8转换成时间戳时间
# 3.请将当前时间的当前月1号的时间戳时间取出来 - 函数
# 4.计算时间差
# 2018-8-19 22:10:8 2018-8-20 11:07:3
# 经过了多少时分秒
# 1.查看一下2000000000时间戳时间表示的年月日 print("第一题结果") print(time.ctime(2000000000)) # 2.将2008-8-8转换成时间戳时间 print("第二题结果") print(time.mktime(time.strptime("2008-8-8","%Y-%m-%d"))) # 3.请将当前时间的当前月1号的时间戳时间取出来 - 函数 def getDate(): st=time.localtime() st2=time.strptime("%s-%s-1"%(st.tm_year ,st.tm_mon),"%Y-%m-%d") return time.mktime(st2) print("第三题结果") print(getDate()) # 4.计算时间差 # 2018-8-19 22:10:8 2018-8-20 11:07:3 # 经过了多少时分秒 st=time.mktime(time.strptime("2018-8-19 22:10:8","%Y-%m-%d %X")) # print(st) st1=time.mktime(time.strptime("2018-8-20 11:07:3","%Y-%m-%d %X")) # print(st1) sub=st1-st gm_time=time.gmtime(sub) # print(gm_time) print("第四题答案") print("过去了%s年%s月%s天%s时%s分%s秒"%(gm_time.tm_year-1970,gm_time.tm_mon-1, gm_time.tm_mday-1,gm_time.tm_hour-1,gm_time.tm_min-1,gm_time.tm_sec))
#3.OS模块
os.makedirs('dirname1/dirname2') 可生成多层递归目录 os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 os.remove() 删除一个文件 os.rename("oldname","newname") 重命名文件/目录 os.stat('path/filename') 获取文件/目录信息 os.system("bash command") 运行shell命令,直接显示 os.popen("bash command).read() 运行shell命令,获取执行结果 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd os.path os.path.abspath(path) 返回path规范化的绝对路径 os.path.split(path) 将path分割成目录和文件名二元组返回 os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素 os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素 os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False os.path.isabs(path) 如果path是绝对路径,返回True os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略 os.path.getatime(path) 返回path所指向的文件或者目录的最后访问时间 os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间 os.path.getsize(path) 返回path的大小
#4.json
# dumps loads
# 在内存中做数据转换 :
# dumps 数据类型 转成 字符串 序列化
# loads 字符串 转成 数据类型 反序列化
# dump load
# 直接将数据类型写入文件,直接从文件中读出数据类型
# dump 数据类型 写入 文件 序列化
# load 文件 读出 数据类型 反序列化
# json是所有语言都通用的一种序列化格式
# 只支持 列表 字典 字符串 数字
# 字典的key必须是字符串
import json
# dic = {'key' : 'value','key2' : 'value2'}
# ret = json.dumps(dic) # 序列化
# with open('json_file','a') as f:
# f.write(ret)
# 从文件中读取字典
# with open('json_file','r') as f:
# str_dic = f.read()
# dic = json.loads(str_dic)
# print(dic.keys())
# dump load 是直接操作文件的
# dic = {'key1' : 'value1','key2' : 'value2'}
# with open('json_file','a') as f:
# json.dump(dic,f)
# with open('json_file','r') as f:
# dic = json.load(f)
# print(dic.keys())
# 问题5 不支持连续的存 取
# dic = {'key1' : 'value1','key2' : 'value2'}
# with open('json_file','a') as f:
# json.dump(dic,f)
# json.dump(dic,f)
# json.dump(dic,f)
# with open('json_file','r') as f:
# dic = json.load(f)
# print(dic.keys())
# 需求 :就是想要把一个一个的字典放到文件中,再一个一个取出来???
# dic = {'key1' : 'value1','key2' : 'value2'}
#
# with open('json_file','a') as f:
# str_dic = json.dumps(dic)
# f.write(str_dic+'\n')
# str_dic = json.dumps(dic)
# f.write(str_dic + '\n')
# str_dic = json.dumps(dic)
# f.write(str_dic + '\n')
# with open('json_file','r') as f:
# for line in f:
# dic = json.loads(line.strip())
# print(dic.keys())
#5.pickle
# 支持在python中几乎所有的数据类型
dic = {(1,2,3):{'a','b'},1:'abc'}
# ret = pickle.dumps(dic)
# print(ret)
#2. dumps 序列化的结果只能是字节
# print(pickle.loads(ret))
# 3.只能在python中使用
# 4.在和文件操作的时候,需要用rb wb的模式打开文件
# 5.可以多次dump 和 多次load
# dump
with open('pickle_file','wb') as f:
pickle.dump(dic,f)
# load
# with open('pickle_file','rb') as f:
# ret = pickle.load(f)
# print(ret,type(ret))
# dic = {(1,2,3):{'a','b'},1:'abc'}
# dic1 = {(1,2,3):{'a','b'},2:'abc'}
# dic2 = {(1,2,3):{'a','b'},3:'abc'}
# dic3 = {(1,2,3):{'a','b'},4:'abc'}
# with open('pickle_file','wb') as f:
# pickle.dump(dic, f)
# pickle.dump(dic1, f)
# pickle.dump(dic2, f)
# pickle.dump(dic3, f)
# with open('pickle_file','rb') as f:
# ret = pickle.load(f)
# print(ret,type(ret))
# ret = pickle.load(f)
# print(ret,type(ret))
# ret = pickle.load(f)
# print(ret, type(ret))
# ret = pickle.load(f)
# print(ret, type(ret))
# ret = pickle.load(f)
# print(ret, type(ret))
# with open('pickle_file','rb') as f:
# while True:
# try:
# ret = pickle.load(f)
# print(ret,type(ret))
# except EOFError:
# break


浙公网安备 33010602011771号