Python持久存储--数据保存到文件
2017-05-05 09:56
477 查看
将基于内存的数据存储到磁盘上,这正是持久存储的含义。
运行结果:
['Hi.', 'How are you?', "I'm file,too.", 'Thanks.']
['Hi.', "I'm fine,thank you.And you?", 'Okay.hi:']
==========================================================
Python的open()BIF除了可以打开文件来读文件,当然也可以写文件,不是吗?
使用open()BIF打开磁盘文件时,可以指定使用什么访问模式。默认地,open()使用模式r表示读,所以不需要专门指定r模式,要打开一个文件完成写,需要使用模式w:
out = open("data.out","w")
默认地,print()BIF显示数据时会使用标准输出(通常是屏幕)。要把数据写至一个文件,需要使用file参数来指定所使用的数据文件对象。
print("Today is a good day.",file= out)
完成工作时,一定要关闭文件,确保所有数据都写至磁盘。这称为刷新输出(flushing)。
out.close()
r:读。r+:读写。不创建。覆盖写。
w:写。w+:读写。创建。覆盖写。
a:写。a+: 读写。创建。附加写。
out.seek(0)
out.read()
解决方案:读取之前将指针重置为文件头。
不论什么情况,finally块儿总会执行。
try: data = open('scatch.txt') man = [] woman = [] for each_line in data: try: (role,line_spoken) = each_line.split(':',1) line_spoken = line_spoken.strip() if role == 'Man': man.append(line_spoken) elif role == 'Woman': woman.append(line_spoken) except ValueError: pass print(man) print(woman) data.close() except IOError: print("The file is missing!")
运行结果:
['Hi.', 'How are you?', "I'm file,too.", 'Thanks.']
['Hi.', "I'm fine,thank you.And you?", 'Okay.hi:']
==========================================================
Python的open()BIF除了可以打开文件来读文件,当然也可以写文件,不是吗?
使用open()BIF打开磁盘文件时,可以指定使用什么访问模式。默认地,open()使用模式r表示读,所以不需要专门指定r模式,要打开一个文件完成写,需要使用模式w:
out = open("data.out","w")
默认地,print()BIF显示数据时会使用标准输出(通常是屏幕)。要把数据写至一个文件,需要使用file参数来指定所使用的数据文件对象。
print("Today is a good day.",file= out)
完成工作时,一定要关闭文件,确保所有数据都写至磁盘。这称为刷新输出(flushing)。
out.close()
r:读。r+:读写。不创建。覆盖写。
w:写。w+:读写。创建。覆盖写。
a:写。a+: 读写。创建。附加写。
out.seek(0)
out.read()
解决方案:读取之前将指针重置为文件头。
try: data = open('scatch.txt') man = [] woman = [] for each_line in data: try: (role,line_spoken) = each_line.split(':',1) line_spoken = line_spoken.strip() if role == 'Man': man.append(line_spoken) elif role == 'Woman': woman.append(line_spoken) except ValueError: pass data.close() except IOError: print("The file is missing!") try: out_man = open("man_data.txt","w+") out_woman = open("woman_data.txt","a+") print(man,file=out_man) print(woman,file = out_woman) out_man.seek(0) out_woman.seek(0) out_man.read() out_woman.read() out_man.close() out_woman.close() except IOError: print("File error")改进后:
try: out_man = open("man_data.txt","w+") out_woman = open("woman_data.txt","a+") print(man,file=out_man) print(woman,file = out_woman) out_man.seek(0) out_woman.seek(0) out_man.read() out_woman.read() except IOError: print("File error") finally: out_man.close() out_woman.close()
不论什么情况,finally块儿总会执行。
相关文章推荐
- HeadFirstPython---------(二)第四章,持久存储,数据保存到文件
- Head First Python 第二章 函数模块&第三章 文件与异常&第四章 持久存储&第五章 处理数据
- 四、持久存储 ----- 数据保存到文件
- 【Python】数据持久存储:pickel
- [python]通达信历史日线数据解析转换为CSV文件进行存储
- python数据持久存储:pickle模块的基本使用
- 利用python进入数据分析之数据加载、存储、文件格式
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- python数据分析之:数据加载,存储与文件格式
- Python Scrapy学习之pipelines不能保存数据到文件问题
- 数据储存(1):文件存储,保存至/data/data/"package_name"/files/目录下
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android存储数据之一:文件保存与读取
- 保存数据到文件 save data to file python2.6和python3中print用法
- Python 数据分析(一) 本实验将学习 pandas 基础,数据加载、存储与文件格式,数据规整化,绘图和可视化的知识
- Python -- 数据加载、存储与文件格式
- Python中数据加载、存储与文件格式
- 从零学python系列之从文件读取和保存数据(转)
- python数据持久存储:pickle模块的基本使用
- Python备份文件、文件版本的学生管理系统如何实现(将数据保存在txt文件中)