详解python持久化文件读写

作者:Daecy 时间:2023-09-01 15:16:44 

持久化文件读写:


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持久化文件读写详解整合网站的支持!

来源:https://blog.csdn.net/weixin_44607176/article/details/88667106

标签:python,文件读写
0
投稿

猜你喜欢

  • python爬虫入门教程--利用requests构建知乎API(三)

    2022-12-17 14:42:45
  • python实现电子书翻页小程序

    2022-11-16 11:10:29
  • Python super()方法原理详解

    2023-06-19 18:28:30
  • python实现自动登录人人网并访问最近来访者实例

    2022-11-30 17:09:15
  • sqlserver关于分页存储过程的优化【让数据库按我们的意思执行查询计划】

    2011-09-30 11:09:37
  • Python中os和shutil模块实用方法集锦

    2021-04-19 08:45:12
  • MySQL对window函数执行sum函数可能出现的一个Bug

    2024-01-23 23:37:42
  • JS中把函数作为另一函数的参数传递方法(总结)

    2024-05-05 09:14:39
  • python copy模块中的函数实例用法

    2022-02-08 09:22:00
  • 分享Pandas库中的一些宝藏函数transform()

    2022-05-03 17:24:44
  • python使用matplotlib模块绘制多条折线图、散点图

    2021-07-28 06:41:20
  • MySQL数据库INNODB表损坏修复处理过程分享

    2024-01-16 10:59:56
  • 解决Linux系统中python matplotlib画图的中文显示问题

    2023-12-18 22:10:53
  • CSS expression在IE8里正式退出历史舞台

    2008-10-26 16:57:00
  • Python 删除List元素的三种方法remove、pop、del

    2021-06-14 20:58:52
  • 总结python多进程multiprocessing的相关知识

    2022-12-04 00:17:57
  • 批处理写的 oracle 数据库备份还原工具

    2024-01-25 06:32:27
  • 浅析python连接数据库的重要事项

    2024-01-26 19:05:07
  • Python项目管理Git常用命令详图讲解

    2021-01-24 13:41:38
  • 基于element-ui中el-select下拉框选项过多的优化方案

    2023-07-02 17:00:50
  • asp之家 网络编程 m.aspxhome.com