Python第三课——初探文件与异常 学习的主要是从文件读取数据、异常处理基本语法
本节课学习如何使用Python向文本文件中写入数据、异常处理的深入补充
1、创建文件,并将需要持久化得数据写入文件中。
'''将上课demo中的谈话内容(conversations)按角色(role)的不同,分别存入两个文本文件中'''
man = [] #分别定义两个list 用来存储两个role的conversations
other = []
try:
data = open('sketch.txt')
try:
for each_line in data:
(role, line_spoken) = each_line.split(':', 1)
line_spoken = line_spoken.strip()
if role == 'man': #通过判断role来确定要存入的list
man.append(line_spoken)
else:
other.append(line_spoken)
except ValueError:
pass
data.close() #别忘了完成文件操作关闭数据文件对象
except IOError:
print('The file is missing!')
try:
man_file = open('man_data.txt', 'w') #数据文件对象中的文件参数如果不存在,并且相应目录有相应权限,open()会自动创建文件
other_file = open('other_data.txt', 'w') # 'w'为文件数据对象的'写'模式
print(man, file = man_file) #print()函数中的file参数为写入的文件名
print(other, file = other_file)
man_file.close() #别忘了完成文件操作关闭数据文件对象
other_file.close()
except IOError:
print('File Error!')
2、改进上面代码中的异常处理逻辑和代码:
上面代码中的异常处理方式依旧是不完善的,想想看,如果在man_file.close()语句之前,代码发生了错误,那么数据文件对象是不会被关闭掉的。
改进代码:
man = []
other = []
try:
data = open('sketch.txt')
try:
for each_line in data:
(role, line_spoken) = each_line.split(':', 1)
line_spoken = line_spoken.strip()
if role == 'man':
man.append(line_spoken)
else:
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError as ioerr: #将IOError异常对象赋予ioerr变量
print('File Error :' + str(ioerr)) #将ioerr转换为字符串类型
try:
man_file = open('man_data.txt', 'w')
other_file = open('other_data.txt', 'w')
print(man, file = man_file)
print(other, file = other_file)
except IOError as ioerr:
print('File Error: ' + str(ioerr))
finally: #无论try代码块或者except代码块中的语句是否被执行,finally代码块中的语句
if 'man_file' in locals(): #判断数据文件对象是否存在,loclas() BIF返回作用域中所有变量的字典
man_file.close()
if 'man_file' in locals():
man_file.close()
3、Python中 文件处理的语法糖:
利用with语句,可以将文件处理的代码简化,无需考虑关闭文件,裁剪掉文件异常处理语句中的finally语句。
作用一:简化语法,减少工作量。
作用二:通过逻辑抽象,减少码农脑细胞死亡速度和出错概率。
对以上代码第二段之改进:
try:
with open('man_data.txt', 'w') as man_file:
print(man, file = man_file)
with open('other_data.txt', 'w') as other_file:
print(other, file = other_file)
except IOError as ioerr:
print('File Error: ' + str(ioerr))
OR
try:
with open('man_data.txt', 'w') as man_file, open('other_data.txt', 'w') as other_file:
print(man, file = man_file)
print(other, file = other_file)
except IOError as ioerr:
print('File Error: ' + str(ioerr))
分享到:
相关推荐
清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT课件-第6章 Python函数.pptx 清华大学精品Python学习PPT课件-...
清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT课件-第6章 Python函数.pptx 清华大学精品Python学习PPT课件-...
知识与技能:了解Python标准库和第三方库的使用,掌握Python第三方库的安装和加载。 2. 过程与方法:通过自主讨论、合作探究,提高自我学习的能力。 3. 情感态度与价值观:了解Python标准库和第三方库的基础知识,...
清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT课件-第6章 Python函数.pptx 清华大学精品Python学习PPT课件-...
清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT课件-第6章 Python函数.pptx 清华大学精品Python学习PPT课件-...
版本2.0与3.0区别: 存在版本兼容问题 外壳(shell): 键入文本与程序交互的界面 python提示符(>>>): 等待键入python指令 Python程序设计-第一课-初识python初中生全文共11页,当前为第4页。 启动IDLE窗口 PART/...
Python电子教案7-1-文件和数据格式化.ppt该文档详细且完整,值得借鉴下载使用,欢迎下载使用,有问题可以第一时间联系作者~
Python课程2.1-使用numpy和pandas实现金融数据的高效计算.pdf Python课程2.2-Matplotlib可视化:绘制2D和3D图.pdf Python课程3.1-爬虫环境配置与简单爬取程序实现.pdf Python课程3.2-典型爬虫程序的实现(1).pdf ...
清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT课件-第6章 Python函数.pptx 清华大学精品Python学习PPT课件-...
Python数据分析与机器学习-交易数据异常检测; Python数据分析与机器学习-交易数据异常检测
清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT课件-第6章 Python函数.pptx 清华大学精品Python学习PPT课件-...
清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT课件-第6章 Python函数.pptx 清华大学精品Python学习PPT课件-...
清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT课件-第6章 Python函数.pptx 清华大学精品Python学习PPT课件-...
Python-Data-Visualization-Cookbook python数据可视化第2版 高清版带目录 pdf电子书
Python课程2.1-使用numpy和pandas实现金融数据的高效计算.pdf Python课程2.2-Matplotlib可视化:绘制2D和3D图.pdf Python课程3.1-爬虫环境配置与简单爬取程序实现.pdf Python课程3.2-典型爬虫程序的实现(1).pdf ...
jmu-python-字符串异常处理.txt
《Python数据分析与应用案例教程》教案---第6课--Pands库(一) 《Python数据分析与应用案例教程》教案---第6课--Pands库(一)全文共9页,当前为第1页。《Python数据分析与应用案例教程》教案---第6课--Pands库(一)全文...
本章为该课程的其中一个章节 【全部课程列表】 第1章-程序设计基本方法 共45页.pptx ...第6章-文件和数据格式化 共54页.ppt 第7章-Python标准库概览 共47页.ppt 第8章-Python第三方库概览 共60页.ppt
【Python 第4课】输入 12 【Python 第5课】变量 14 【Python 第6课】bool 16 【Python 第7课】if 19 【Python 第8课】while 23 【Python 第9课】random 26 【Python 第10课】变量2 28 【Python 第11课】逻辑判断 29 ...
python 20、爬虫 08-1_Scrapy中数据持久化存储、多级子页面爬取_Day08_am.mp4