一:学习内容
- 列表元组字典集合文件操作
二:列表元组字典集合文件操作
代码:
import pickle #数据持久性模块
#封装的方法
def OptionData(data,path):
#写文件
f = open(path, "wb")
pickle.dump(data,f)
f.close()
#读文件
f1 = open(path,"rb")
tempList = pickle.load(f1)
print(tempList)
f1.close()
path = r"F:\python_test\data.txt"
#1.列表文件写入和读取
myList = [1,2,3,4,5,"tester is a good girl"]
OptionData(myList,path)
#2.元组文件写入和读取
myTuple = (1,2,3,4,5,"tester is a good girl")
OptionData(myTuple,path)
#3.字典文件写入和读取
myDict = {"name":"测试人员","age":18}
OptionData(myDict,path)
#4.集合文件写入和读取
mySet = set([1,2,3,4,5,"tester is a good girl"])
OptionData(mySet,path)
运行结果:会生成一个data.txt将数据写入进去,并打印写入的内容