1、pickle
可以将我们python中的任意数据类型转化成bytes并写入到文件中. 同样也可以把文件中写好的bytes转换回我们python的数据.
pickle可以直接序列化对象.
class Fruit:
def __init__(self,name,color):
self.name = name
self.color = color def eat(self):
return "haochi" p1 = Fruit("apple1","red1")
p2 = Fruit("apple2","red4")
p3 = Fruit("apple3","red2")
p4 = Fruit("apple4","red6")
lst = [p1,p2,p3,p4] pickle.dump(lst,open("fruit_info","wb")) lst = pickle.load(open("fruit_info","rb"))
for i in lst:
print(i.name)
pickle序列化对象
2、shelve
shelve提供python的持久化操作. 什么叫持久化操作呢? 说白话,就是把数据写到硬盘上. 在操作shelve的时候非常的像操作一个字典. 这个东西到后期. 就像redis差不多.
# shelve 数据的持久化操作, 像操作字典一样去操作shelve -----------------------------------------------------------------
d = shelve.open("shelve_file")
d["wlh"] = "王力宏"
d["ljj"] = {"name":"林俊杰","id":"singer"}
d["zj"] = {"name":"张杰","wife":{"name":"谢娜","gender":"女","worke":"大本营"}}
d.close() d = shelve.open("shelve_file")
d.pop("zj") # 能删除,但是再查询时会报错.
d.close() d = shelve.open("shelve_file",writeback=True) # 如果是嵌套字典的话, 必须得加上writeback回写参数, 不然没法修改
d["ljj"]["id"] = "歌手"
print(d["ljj"]["id"])
d.close()
shelve
3、configparser
该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)
import configparser
config = configparser.ConfigParser()
config["DEFAULT"] = { # DEFAULT 是默认配置, 下面的一些子配置都会带上DEFAULT 里面的配置.
"默认设置1":"...1",
"默认设置2":"....2",
"默认设置3":".....3",
}
config["配置11"] = {
"设置11":"",
"设置12":"",
"设置13":"",
}
config["配置22"] = {
"设置21":"",
"设置22":"",
"设置23":"",
}
config["配置33"] = {
"设置31":"",
"设置32":"",
"设置33":"",
} with open("peizhi.ini","w",encoding="utf8") as f:
config.write(f) # 这里的write()方法是configparser自己重新写的,不是调用的文件操作的write() # 查
config = configparser.ConfigParser()
config.read("peizhi.ini",encoding="utf8")
print(config.sections()) # ['配置11', '配置22', '配置33']
print(config["配置33"]["设置33"]) # # 添加一个章节 先读取文件, 再修改, 最后写入文件
config = configparser.ConfigParser()
config.read("peizhi.ini",encoding="utf8")
config.add_section("配置44")
config["配置44"] = {
"设置41": "",
"设置42": "",
"设置43": "",
}
config.write(open("peizhi.ini","w",encoding="utf8")) # 修改章节
config = configparser.ConfigParser()
config.read("peizhi.ini",encoding="utf8")
config.set("配置44","设置43","")
config.write(open("peizhi.ini","w",encoding="utf8")) # 删除章节或 章节元素
config = configparser.ConfigParser()
config.read("peizhi.ini",encoding="utf8")
config.remove_section("配置44") # 删除"配置44"章节
config.remove_option("配置44","设置43") # 删除"配置44"里的"设置43"元素
config.write(open("peizhi.ini","w",encoding="utf8")) # 对config的所有修改操作都是先创建config对象,然后用config读取内容, 修改完之后再将config写进文件.
configparser
4、json
json是所有语言都通用的模块
4.1 json和pickle的比较?
dic = {1:"青铜",2:"白银",3:"黄金",4:"铂金",5:"钻石"} ret = json.dumps(dic,ensure_ascii=False) # 用 ensure_ascii=False 来处理非中文显示
print(ret,type(ret)) # json结果是 str 类型
# ret = pickle.dumps(dic)
# print(ret,type(ret)) # pickle结果是 bytes 类型
4.2 json写进文件
dic = {'王力宏':{"ji":"新加坡","gender":"男","album":"盖世英雄"},
"林俊杰":"爱笑的眼睛",
"张靓颖":{"gender":"女","album":"如果这就是爱情"},
"张杰":"云中的angle"
}
with open("singer_info.json","w",encoding="utf8") as f:
json.dump(dic,f,ensure_ascii=False,indent=4) # indent=4 即在json文件中改成好看点的格式
# 即下面格式:
# {
# "王力宏": {
# "ji": "新加坡",
# "gender": "男",
# "album": "盖世英雄"
# },
# "林俊杰": "爱笑的眼睛",
# "张靓颖": {
# "gender": "女",
# "album": "如果这就是爱情"
# },
# "张杰": "云中的angle"
# }
json数据写进文件
4.3 json也能处理对象, 也能将对象写进文件(通过间接方式将对象转成字典的形式,然后序列化字典写进文件)
class Fruit:
def __init__(self,name,color):
self.name = name
self.color = color def trans1(obj):
return {
"name": obj.name,
"color": obj.color,
}
# 用json序列化对象 ???
ap = Fruit("苹果","红色") # 方法一: __dict__ 对obj的各种封装信息(也是字典)进行序列化
# ret = json.dumps(ap.__dict__, ensure_ascii=False)
# print(ret) # 方法二: 对函数返回的结果进行序列化
ret = json.dumps(ap,default=trans1,ensure_ascii=False) # 把obj对象作为参数传进trans里,将返回的字典结果进行序列化
print(ret)
json序列化对象
class Fruit:
def __init__(self,name,color):
self.name = name
self.color = color def trans2(dic):
print(type(dic))
return Fruit(dic["name"],dic["color"]) # 返回自己构造的Fruit对象 # 用json反序列化
s = '{"name": "苹果", "color": "红色"}'
ap = json.loads(s,object_hook=trans2) # 先将s进行反序列化, 然后将反序列化结果作为参数传进trans2 ,在函数里自己构造对象
print(ap.color)
json反序列化对象
4.4 如何将大量数据写进文件?
不用 dump. 改用dumps和loads. 对每一行分别进行处理.
用dumps将数据逐个序列化, 并写进文件; 用loads逐行将字符串反序列化, 并输出(不能用load, 逐行读出来的字符串是不符合json语法规范, 没法load, 会直接报错).
# 用json写进大量数据
dic1 = {"name":"乱世佳人","show_time":1975,"score":8.5,"time":"180min"}
dic2 = {"name":"闻香识女人","show_time":1998,"score":8.8,"time":"160min"}
dic3 = {"name":"飞越疯人院","show_time":1973,"score":8.7,"time":"140min"}
dic4 = {"name":"霸王别姬","show_time":2001,"score":9.3,"time":"150min"}
lst = [dic1,dic2,dic3,dic4]
# with open("movie.json","w",encoding="utf8") as f:
# for i in lst:
# s = json.dumps(i,ensure_ascii=False)
# f.write(s+"\n") with open("movie.json","r",encoding="utf8") as f:
for line in f:
line = line.strip()
if not line:
continue
cont = json.loads(line) # 只是一个反序列化操作, 把字符串式的line转成字典形式.
# cont = json.load(line) # 错误写法, 这里line只是读取了json文件的一行,并没有全部读取,
# line里面所存的字符串是不符合json语法的,没法load, 所以读取出错.
print(cont)
json写入大量数据