os模块补充以及序列化模块
一、os模块的补充
1.os.path.abspath 能把存在的相对路径的绝对路径显示出来
path = os.path.abspath("连达day19.py") print(path) #F:\pythonworkspace\day19\连达day19.py
2.os.path.split 把一个路径分成两段(以元组的形式展示),第二段是该路径的最后一个文件或者文件夹
path = os.path.split("F:\pythonworkspace\day19\连达day19.py") print(path) #('F:\\pythonworkspace\\day19', '连达day19.py')
3.os.path.dirname
os.path.basename
ret1 = os.path.dirname("F:\pythonworkspace\day19\连达day19.py") ret2 = os.path.basename("F:\pythonworkspace\day19\连达day19.py") print(ret1) print(ret2) # F:\pythonworkspace\day19 # 连达day19.py
4.os.path.exists 判断文件或者文件夹是否存在
ret = os.path.exists("F:\pythonworkspace\day19\连达day19.py") print(ret) ret = os.path.exists("F:\pythonworkspace\day19\连达da19.py") print(ret) # True # False
5.os.path.isabs 判断是否为绝对路径
ret1 = os.path.isabs("F:\pythonworkspace\day19\连达da19.py") ret2 = os.path.isabs("连达da19.py") print(ret1) print(ret2) # True # False
6.os.path.isdir os.path.isfile 判断是否为文件夹或者文件
ret1 = os.path.isdir("F:\pythonworkspace\day19") ret2 = os.path.isfile("连达da19.py") print(ret1) print(ret2) # True # False
7.os.path.join 路径的拼接
path = os.path.join("F:\pythonworkspace\day19","连达da19.py") 拼接的可以是不存在的路径 print(path) # F:\pythonworkspace\day19\连达da19.py
8.os.path.getsize 获取文件的大小,单位是字节(bytes)
size = os.path.getsize("F:\pythonworkspace\day19\连达day19.py") print(size) #1775
9.os.listdir 以列表的形式把当前路径下的所有文件或文件夹名称显示出来
ret = os.listdir("F:\pythonworkspace") print(ret) # ['.idea', 'day01', 'day02', 'day03', 'day04', 'day05', 'day06', 'day07', 'day08', 'day09', 'day10', 'day11', 'day13', 'day14', 'day15', 'day16', 'day17', 'day18', 'day19']
二、序列化模块
序列:列表、元组、字符串、bytes
序列化:字符串、bytes 把其他数据类型转换成字符串、bytes的过程叫序列化,反之成为反序列化
1.json模块:
json.dumps() #序列化
dic = {"k1":"v1","k2":"v2","k3":"v3"} ret = json.dumps(dic) print(ret,type(ret)) #{"k1": "v1", "k2": "v2", "k3": "v3"} <class 'str'>
json.loads() #反序列化
dic = {"k1":"v1","k2":"v2","k3":"v3"} ret = json.dumps(dic) ret = json.loads(ret) print(ret,type(ret)) #{'k1': 'v1', 'k2': 'v2', 'k3': 'v3'} <class 'dict'>
*1.当序列化过程中有int类型会转化成字符串类型,但是在反序列化过程中不会再转换成int类型
dic = {1 : 'value',2 : 'value2'} ret = json.dumps(dic) print(dic,type(dic)) print(ret,type(ret))
res = json.loads(ret)
print(res,type(res))
#{1: 'value', 2: 'value2'} <class 'dict'>
#{"1": "value", "2": "value2"} <class 'str'>
#{'1': 'value', '2': 'value2'} <class 'dict'>
*2.当序列化过程中元组会变成列表,但是反序列化不会变回元组
dic = {1 : [1,2,3],2 : (4,5,'aa')} ret = json.dumps(dic) print(dic,type(dic)) print(ret,type(ret)) res = json.loads(ret) print(res,type(res)) #{1: [1, 2, 3], 2: (4, 5, 'aa')} <class 'dict'> #{"1": [1, 2, 3], "2": [4, 5, "aa"]} <class 'str'> #{'1': [1, 2, 3], '2': [4, 5, 'aa']} <class 'dict'>
*3.集合不能被序列化
s = {1,2,"aa"} ret = json.dumps(s) print(ret) #报错
*4.字典序列化key不能是元组
***json能够处理的数据类型是非常有限的:字符串、列表、字典、int
字典中的key只能是字符串(如果是init就换成str)
序列化与文件的相关操作:
#将字典序列化之后写入文件
dic = {'key' : 'value','key2' : 'value2'} ret = json.dumps(dic) with open("json_file","w") as f: f.write(ret) f.close()
#将文件中的字符串读出来,然后反序列化变成字典
with open("json_file") as f: str = f.read() f.close() ret = json.loads(str) print(ret) #{'key': 'value', 'key2': 'value2'}
dump和load 是直接操作文件的
dic = {'key1' : 'value1','key2' : 'value2'} with open("json_file","a") as f: json.dump(dic,f) #注意有两个参数,一个是要序列化的对象,第二个是文件的句柄
with open("json_file") as f: #该文件中只有一个字典才能读出来 dic = json.load(f) print(dic)
实现了多次存取
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())
data = {'username':['李华','二愣子'],'sex':'male','age':16} json_dic2 = json.dumps(data,sort_keys=True,indent=4,separators=(',',':'),ensure_ascii=False)
字典的key 格式 #用来显示汉字
pickle模块
pickle模块的功能与用法与json基本一致
*支持python中几乎所有的数据类型
*序列化的结果只能是字节
*只能在python中使用
*在和文件操作的时候,要用“rb”,“wb”的模式
*可以多次dump和load
with open('pickle_file','rb') as f: while True: try: ret = pickle.load(f) print(ret,type(ret)) except EOFError: break