Python pickle序列化后写入文件,并实现曾加内容
2020-12-13 04:11
标签:print src coding click %s else imp spl 字符 pickle序列化 代码运行: Python pickle序列化后写入文件,并实现曾加内容 标签:print src coding click %s else imp spl 字符 原文地址:https://www.cnblogs.com/pengingintent/p/11105716.html# _*_coding:utf-8_*_
import pickle
import os
list_ec = []
while True:
if os.path.isfile("course_list"): # 判断文件夹是否存在
with open("course_list", "rb") as f:
list_aa = pickle.load(f)
f.close()
# print(list_aa) # 可以打开,每次读取完文件的时候,打印出来
nu = input(">>") # 输入列表内每个元素的首字符以"|"为分割符
if nu == "exit":
break
else:
for i in list_aa:
list_ec.append(i)
if nu == i.split("|")[0]:
number_a = list_aa.index(i)
# print(number_a) # 打印出每次修改元素所在列表内的索引
ni = input(">:") # 输入增加的字符
nui = list_ec[number_a]
list_ec[number_a] = "%s|%s" % (nui, ni)
print(list_ec) # 输入完成后,打印出修改完成的字符
with open("course_list", "wb") as f:
pickle.dump(list_ec, f)
f.close()
else:
"""
文件夹不存在先把下面的信息写入创建文件的夹内
"""
a = "Lili|python|linux"
b = "admin|linux|python"
c = "ak|python|go"
list_a = [a, b, c]
print(list_a)
with open("course_list", "wb") as f:
pickle.dump(list_a, f)
f.close()
C:\Python35\python.exe C:/Users/pengi/Desktop/6-13/class_s.py
[‘Lili|python|linux‘, ‘admin|linux|python‘, ‘ak|python|go‘]
>>ak
>:linux
[‘Lili|python|linux‘, ‘admin|linux|python‘, ‘ak|python|go|linux‘]
文章标题:Python pickle序列化后写入文件,并实现曾加内容
文章链接:http://soscw.com/essay/29116.html