序列化
什么是序列化 -- json
lit = [1, 2, 3, 4, 5,]
_lit= str(lit)
print(_lit)
print(list(_lit))
print(eval(_lit))
import json
两组四个方法
1.dumps loads
2.dump load
lit = [1, 2, 3, 4, 5, ]
_lst = json.dumps(lit)
print(_lst,type(_lst))
lis = json.loads(_lst)
print(lis,type(_lst))
lit = {'A': 1, 'B': 2, 'C': 3, }
_lst = json.dumps(lit)
_lst = json.dumps(lit,ensure_ascii=False)
print(_lst, type(_lst))
lis = json.loads(_lst)
print(lis, type(_lst))
import json
lit = {'A': 1, 'B': 2, 'C': 3, }
lit = [1, 2, 3, 4, 5, 6]
f = open('info', 'a', encoding='utf-8')
json.dump(lit, f)
f.close()
f1 = open("info", 'r', encoding="utf-8")
for i in f1:
i = json.loads(i)
print(i)
f1.close()
dump:将对象转换(序列化)成字符串,写入文件
liad:
pickle 序列化--(python所有对象进行转换)
唯一的区别是转换成特殊符号
import pickle
def func():
print(123)
lit = [1, 2, 3, 4, 5, 6]
_list= pickle.dumps(lit)
print(_list)
print(pickle.loads(func()))
什么是序列化 -- json
lit = [1,22,3,3,45] # [1,22,3,3,45]
s_lst = str(lit)
print(s_lst,type(s_lst))
print(list(s_lst))
print(eval(s_lst)) # 禁止使用
import json
两组4个方法:
1.dumps loads
lit = [1,22,3,3,45]
print(json.dumps(lit),type(json.dumps(lit)))
str_lst = json.dumps(lit)
lst = json.loads(str_lst)
print(lst,type(lst))
dumps 将对象转换(序列化)成字符串
loads 将字符串转换(反序列化)成对象
dic = {'username':'宝元'}
str_dic = json.dumps(dic) # 序列化
str_dic = json.dumps(dic,ensure_ascii=False) # ensure_ascii=False 关闭ascii码
print(str_dic,type(str_dic))
dic1 = json.loads(str_dic) # 反序列化
print(dic1,dic1["username"])
2.dump load
import json
lit = [1,22,3,3,45]
同时写多个内容 进行序列化
lst = [1,2,3,4,56,]
f = open("info","w",encoding="utf-8")
f.write(json.dumps(lst) + "\n")
f.write(json.dumps(lst) + "\n")
f.write(json.dumps(lst) + "\n")
f.write(json.dumps(lst) + "\n")
f.write(json.dumps(lst) + "\n")
f.close()
dump: 将对象转换(序列化)成字符串,写入文件
load: 将文件中字符串转换(反序列)成对象
同时读多个内容进行反序列
f1 = open("info","r",encoding="utf-8")
for i in f1:
l = json.loads(i)
print(l)
f1.close()
pickle 序列化 - nb(python所有对象进行转换)
python自带的(只有python可以用)
两组4个方法:
1. dumps loads
import pickle
lst = [12,3,4,5,768]
t_list = pickle.dumps(lst) # 转换成类似字节
print(t_list)
print(pickle.loads(t_list)[-1])
dic = {"user":"郭宝元"}
t_list = pickle.dumps(dic) # 转换成类似字节
print(t_list)
print(pickle.loads(t_list))
def func():
print(111)
import json
fun = json.dumps(func)
print(fun)
fun = pickle.dumps(func)
print(fun)
pickle.loads(fun)()
2. dump load
import pickle
dic = {"usern":"baoyuian"}
dic = {"usern":"宝元"}
pickle.dump(dic,open("info","wb"))
print(pickle.load(open("info","rb")))
import pickle
dic = {"user":"123"}
pickle.dump(dic,open("info","ab"))
import pickle
dic = {"1":2}
f = open("info","wb")
s = "\n".encode("utf-8")
f.write(pickle.dumps(dic)+ s)
f.write(pickle.dumps(dic)+ s)
f.write(pickle.dumps(dic)+ s)
f.close()
f1 = open("info","rb")
for i in f1:
print(pickle.loads(i))
推荐使用json
json是各种语言通用的
pickle(python私有)
asdfasd
os
import sys
print(sys.platform)
os 模块 -- 程序员通过python向操作系统发送指令(与操作系统交互的接口)
os模块四组:
1.工作目录
import os
print(os.getcwd()) # 当前工作路径 ***
os.chdir("F:\s24\day06") # 路径切换
print(os.curdir) # 当前
print(os.pardir) # 父级
2.文件夹
import os
os.mkdir("ttt") # 创建一个文件夹 ***
os.rmdir("ttt") # 删除一个文件夹 ***
os.makedirs("ttt/sss/ddd/ee") # 递归创建文件夹 ***
os.removedirs("ttt/sss/ddd/ee") # 递归删除文件夹 ***
print(os.listdir(r"F:\s24\day17")) ***
3.文件
import os
os.rename() # 修改名字 ***
os.remove("info") # 删除文件 ***
4.路径
import os
print(os.path.abspath(r"01 今日内容.py")) # 通过相对路径获取绝对路径 ***
print(os.path.split(os.path.abspath(r"01 今日内容.py"))) #将路径以最后一个\切割(路径,文件名)
print(os.path.dirname(r"F:\s24\day17\01 今日内容.py")) # 获取路径 ***
print(os.path.basename(r"F:\s24\day17\01 今日内容.py")) # 获取文件名 **
print(os.path.exists(r"F:\s24\day17\01 今日内容.py")) # 判断这个路径是否存在 ***
print(os.path.isdir(r"F:\s24\day17")) # 判断是不是路径 ***
print(os.path.isfile(r"01 今日内容.py")) # 判断是不是文件 ***
print(os.path.isabs(r"F:\s24\day17\01 今日内容.py")) # 判断是不是绝对路径
print(os.path.join("D:\\\\","ttt","bbb")) # 路径拼接 *****
import time
print(time.time())
print(os.path.getatime(r"F:\s24\day17\04 序列化.py")) # 最后的修改时间
print(os.path.getctime(r"F:\s24\day17\04 序列化.py")) # 最后的访问时间
print(os.path.getmtime(r"F:\s24\day17\04 序列化.py")) # 最后的访问时间
print(os.path.getsize(r"F:\s24\day09")) # 获取当前文件的大小 ***
sys
sys -- 与python解释器交互的接口
import sys
if sys.argv[-1] == "alex":
print("dsb")
else:
print("李业dsb")
print(sys.argv[-1]) # *** 当前文件运行['F:/s24/day17/06 sys.py']
import sys
print(sys.exit(1)) #
import sys
print(sys.version) # 获取解释器版本
import sys
print(sys.path) # 添加自定义模块查找路径 *****
import sys
print(sys.platform) # *** 区分操作系统然后进行相关逻辑操作
hashlib
hashlib 加密
加密和校验
alex:alex123
alex:23lw23jky321jh4gqyt1234gj8b7t # 加密后
{"1234":23lw23jky321jh4gqyt1234gj8b7t}
md5,sha1,sha256,sha512
1.只要明文相同密文就是相同的
2.只要明文不相同密文就是不相同的
3.不能反逆(不能解密) -- md5中国人破解了
alex:alex123
alex:b75bd008d5fecb1f50cf026532e8ae67
print(len("b75bd008d5fecb1f50cf026532e8ae67"))
加密:
1.加密的内容
2.将要加密的内容转成字节
import hashlib
md5 = hashlib.md5()
md5.update("alex123".encode("utf-8"))
print(md5.hexdigest())
md5 = hashlib.md5()
md5.update("alex".encode("utf-8"))
print(md5.hexdigest())
md5 = hashlib.md5()
md5.update("alex123".encode("gbk"))
print(md5.hexdigest())
md5 = hashlib.md5()
md5.update("alex123".encode("shift-jis"))
print(md5.hexdigest())
import hashlib
sha1 = hashlib.sha256()
sha1.update("alex123".encode("utf-8"))
print(sha1.hexdigest())
md5 = hashlib.md5()
md5.update("alex123".encode("utf-8"))
print(md5.hexdigest())
最常用是的md5,平时加密的时候使用sha1
加盐
加固定盐
import hashlib
md5 = hashlib.md5("常鑫".encode("utf-8"))
md5.update("alex123".encode("utf-8"))
print(md5.hexdigest())
md5 = hashlib.md5()
md5.update("alex123".encode("utf-8"))
print(md5.hexdigest())
动态加盐
user = input("username:")
pwd = input("password")
import hashlib
md5 = hashlib.md5(user.encode("utf-8"))
md5.update(pwd.encode("utf-8"))
print(md5.hexdigest())
md5 = hashlib.md5()
md5.update(pwd.encode("utf-8"))
print(md5.hexdigest())
767db14ed07b245e24e10785f9d28e29
f = open(r"F:\s24\day17\python-3.6.6-amd64.exe","rb")
import hashlib
md5 = hashlib.md5()
md5.update(f.read())
print(md5.hexdigest())
ss = "baoyuanalextaibai"
s = "baoyuan"
s1 = "alex"
s2 = "taibai"
import hashlib
md5 = hashlib.md5()
md5.update(ss.encode("utf-8"))
print(md5.hexdigest())
md5 = hashlib.md5()
md5.update(s.encode("utf-8"))
md5.update(s1.encode("utf-8"))
md5.update(s2.encode("utf-8"))
print(md5.hexdigest())
节省内存
f = open(r"F:\s24\day17\python-3.6.6-amd64.exe","rb")
import hashlib
md5 = hashlib.md5()
while True:
msg = f.read(1024)
if msg:
md5.update(msg)
else:
print(md5.hexdigest())
break
collections
1.namedtuple: 生成可以使用名字来访问元素内容的tuple
2.deque: 双端队列,可以快速的从另外一侧追加和推出对象
3.Counter: 计数器,主要用来计数
4.OrderedDict: 有序字典
5.defaultdict: 带有默认值的字典
from collections import namedtuple,deque
Point = namedtuple('tu',["a","b","c"]) # 第一个参数是元组的名字,第二参数是元组中元素的[名字,名字]
p = Point({"keu":(1,2,3,4)}, 20,10)
print(p)
tu = (1,2,32,34,4)
print(tu)
lst = deque([1,2,3,4,5,6,7])
lst.append(8)
lst.appendleft(0)
lst.pop()
lst.popleft()
print(lst[4])
队列:先进先出
栈:先进后出 -- 栈顶
lst = [1,2,3,4]
lst.append(5)
lst.pop(0)
5.defaultdict: 带有默认值的字典
from collections import defaultdict
dic = defaultdict(list)
dic["k1"].append(12)
print(dic)
li = [11,22,33,44,55,77,88,99,90]
result = {"key1":[],"key2":[]}
result = {}
for row in li:
if row > 66:
if 'key1' not in result:
result['key1'] = []
result['key1'].append(row)
else:
if 'key2' not in result:
result['key2'] = []
result['key2'].append(row)
print(result)
from collections import defaultdict
dic = defaultdict(set)
li = [11,22,33,44,55,77,88,99,90]
for i in li:
if i > 66:
dic["k1"].add(i)
else:
dic["k2"].add(i)
print(dic)
from collections import Counter
s = "1112233344aaa"
s = [1,1,2,2,3,3]
s = (1,2,3,3,4,5,6,7,78)
print(dict(Counter(s))) # ***
统计元素出现的次数
原文地址:https://www.cnblogs.com/changxin7/p/11252139.html
时间: 2024-10-09 00:50:01