詳解python持久化文件讀寫
更新時間:2019年04月06日 09:10:09 作者:Daecy
這篇文章主要介紹了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)
以上所述是小編給大家介紹的python持久化文件讀寫詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
使用python實現男神女神顏值打分系統(tǒng)(推薦)
這篇文章主要介紹了用python做一個男神女神顏值打分系統(tǒng)(程序分析見注釋),需要的朋友可以參考下2019-10-10
TensorFlow2.4完成Word2vec詞嵌入訓練方法詳解
這篇文章主要為大家介紹了TensorFlow2.4完成Word2vec詞嵌入訓練方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

