欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

详解python持久化文件读写

时间:2021-04-20 08:54:08|栏目: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编程使用selenium模拟登陆淘宝实例代码

栏    目:Python代码

下一篇:跟老齐学Python之一个免费的实验室

本文标题:详解python持久化文件读写

本文地址:http://www.codeinn.net/misctech/104747.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有