详解python持久化文件读写
持久化文件读写:
f=open('info.txt','a+') f.seek(0) str1=f.read() if len(str1)==0: f1 = open('info.txt', 'w+') str1 = f.read()
# 如果数据没有就写入数据到文件
time_list = ["早上", "中午", "晚上"] character_list = ["小赵","小钱", "小孙", "小李"] place_list = ["在屋里", "在外面", "在学校", "在公司"] event_list = ["吃饭", "遛狗", "看书", "工作"] dic1={'time_list':time_list,'character_list':character_list,'place_list':place_list,'event_list':event_list} str1=str(dic1) f1.write(str1) f1.close() print("文件保存成功")
# 如果数据有,就字符串转换为字典
else: dic1=eval(str1) print(type(dic1)) print(dic1) f=open('info.txt','a+') f.seek(0) str1=f.read() if len(str1)==0: f1 = open('info.txt', 'w+') str1 = f.read()
# 如果数据没有就写入数据到文件
time_list = ["早上", "中午", "晚上"] character_list = ["小赵","小钱", "小孙", "小李"] place_list = ["在屋里", "在外面", "在学校", "在公司"] event_list = ["吃饭", "遛狗", "看书", "工作"] dic1={'time_list':time_list,'character_list':character_list,'place_list':place_list,'event_list':event_list} str1=str(dic1) f1.write(str1) f1.close() print("文件保存成功")
# 如果数据有,就字符串转换为字典
else: dic1=eval(str1) print(type(dic1)) print(dic1)
相关推荐
温攀峰 2020-10-23
Dyancsdn 2020-07-28
鲁氏汤包王 2020-06-08
Callmesmallpure 2020-05-31
无能力者只知抱怨 2020-05-09
技术之博大精深 2020-05-03
shenzhenzsw 2020-05-01
jokewinl 2020-04-20
ljbhander 2020-04-11
JavaWDB 2020-03-26
fansenjun 2020-03-03
wangxiaoxue 2020-02-17
Java高知 2020-02-14
wangxiaoxue 2020-01-29
qiqizhiyun 2020-01-29