ch3:文件处理与异常
如何从文件读入数据?
python中的基本输入机制是基于行的;
python中标准的“打开-处理-关闭”代码:
the_file=open('文件全称')
#处理文件中的数据
the_file.close()
使用IDLE来感受python的文件输入机制;
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import os #从标准库导入os
>>> os.getcwd() #查看当前工作目录
'C:\\Python35-32'
>>> os.chdir('D:\workspace\headfirstpython\chapter3') #改变当前工作目录到包括数据文件的文件夹中
>>> os.getcwd() #确认当前工作目录在正确的目录下
'D:\\workspace\\headfirstpython\\chapter3'
>>> ##打开数据文件,从文件读取前两行,并打印到屏幕上
>>> data=open("sketch.txt') SyntaxError: EOL while scanning string literal
>>> #以上提示结尾处字符串错误,发现是因为引号没有成对使用所致
>>> data=open('sketch.txt')
>>> print(data.readline (), end='') #使用"readline()"方法从文件获取一个数据行
Man: Is this the right room for an argument?
>>> print(data.readline (), end='') #再次运行,获取第二行数据
Other Man: I've told you once.
>>> #####下面再“退回”到文件起始位置,然后使用for循环处理文件中的每一行
>>> data.seek(0) #使用"seek()"方法返回到文件起始位置,当然对于python文件也可以使用“tell()”。
0
>>> for each_line in data:
print(each_line,end='') Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: No you haven't!
Other Man: Yes I have.
Man: When?
Other Man: Just now.
Man: No you didn't!
Other Man: Yes I did!
Man: You didn't!
Other Man: I'm telling you, I did!
Man: You did not!
Other Man: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man: Just the five minutes. Thank you.
Other Man: Anyway, I did.
Man: You most certainly did not!
Other Man: Now let's get one thing quite clear: I most definitely told you!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh look, this isn't an argument!
(pause)
Other Man: Yes it is!
Man: No it isn't!
(pause)
Man: It's just contradiction!
Other Man: No it isn't!
Man: It IS!
Other Man: It is NOT!
Man: You just contradicted me!
Other Man: No I didn't!
Man: You DID!
Other Man: No no no!
Man: You did just then!
Other Man: Nonsense!
Man: (exasperated) Oh, this is futile!!
(pause)
Other Man: No it isn't!
Man: Yes it is!
>>> data.close()
>>>
使用函数汇总:
os.getcwd() #查看当前工作目录
os.chdir('D:\workspace\headfirstpython\chapter3') #改变当前工作目录
data=open('sketch.txt') #获取文件
data.readline() #从文件获取一个数据行
data.seek(0) #使用"seek()"方法返回到文件起始位置
data.close() #关闭文件
进一步查看数据:
遵循特定的格式:“演员角色: 台词”
可以使用split()方法抽取出数据行中需要的各个部分;
split()方法:返回一个字符串列表,这会赋至一个目标标识符列表。这称为“多重赋值”;
(role, line_spoken)=each_line.split(':')
将数据行以“:”进行分隔,分别赋之给role和line_spoken;
说明:split()方法传回的是一个列表,但是目标标识符包括在小括号之间,而非中括号之间。
python有两种列表,一种可以改变的列表(中括号包围);另一种一旦创建就不能改变(小括号包围),常称呼为“元组(tuple)”,可以认为是一个“常量列表”。
>>> data=open('sketch.txt')
>>> for each_line in data:
(role, line_spoken)=each_line.split(':')
print(role,end='')
print(' said: ',end='')
print (line_spoken,end='')
Man said: Is this the right room for an argument?
Other Man said: I've told you once.
Man said: No you haven't!
Other Man said: Yes I have.
Man said: When?
Other Man said: Just now.
Man said: No you didn't!
Other Man said: Yes I did!
Man said: You didn't!
Other Man said: I'm telling you, I did!
Man said: You did not!
Other Man said: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man said: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man said: Just the five minutes. Thank you.
Other Man said: Anyway, I did.
Man said: You most certainly did not!
Traceback (most recent call last):
File "<pyshell#29>", line 2, in <module>
(role, line_spoken)=each_line.split(':')
ValueError: too many values to unpack (expected 2)
错误提示:Man said: You most certainly did not!该句下一句有太多的值进行拆分
分析发现:Other Man: Now let's get one thing quite clear: I most definitely told you!该句有两个冒号,而不是一个冒号,代码没有告诉split()如何处理第二个冒号,导致该方法无法正常工作;
>>> help(each_line.split)
Help on built-in function split: split(...) method of builtins.str instance
S.split(sep=None, maxsplit=-1) -> list of strings Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
split()有一个可选的参数maxsplit,控制着将数据行分解为多个部分。如果将该参数设置为1,数据行只会拆分为两部分,这样就会消除数据行只会额外的冒号的影响;
>>> data=open('sketch.txt')
>>> for each_line in data:
(role, line_spoken)=each_line.split(':',1)
print(role,end='')
print(' said: ',end='')
print (line_spoken,end='')
Man said: Is this the right room for an argument?
Other Man said: I've told you once.
Man said: No you haven't!
Other Man said: Yes I have.
Man said: When?
Other Man said: Just now.
Man said: No you didn't!
Other Man said: Yes I did!
Man said: You didn't!
Other Man said: I'm telling you, I did!
Man said: You did not!
Other Man said: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man said: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man said: Just the five minutes. Thank you.
Other Man said: Anyway, I did.
Man said: You most certainly did not!
Other Man said: Now let's get one thing quite clear: I most definitely told you!
Man said: Oh no you didn't!
Other Man said: Oh yes I did!
Man said: Oh no you didn't!
Other Man said: Oh yes I did!
Man said: Oh look, this isn't an argument!
Traceback (most recent call last):
File "<pyshell#39>", line 2, in <module>
(role, line_spoken)=each_line.split(':',1)
ValueError: not enough values to unpack (expected 2, got 1)
Other Man said: Now let's get one thing quite clear: I most definitely told you!
成功打印到了屏幕上,但是又出现了新的错误,是因为“(pause)”语句格式不符合我们期望的格式所致。 我们发现意外情况越来越多,可以选择两种截然不同的方法:
- 继续增加额外的逻辑对付这些异常;
- 让错误出现,监视他的发生,然后从运行时的错误(以某种方式)恢复;
- 增加额外逻辑:采用字符串的find()方法
find()方法用来查找一个字符串中的子串,如果无法找到,find()方法会返回值-1;如果找到,返回该子串在原字符串中的索引位置。
>>> each_line='I tell you, today is sunday!'
>>> each_line.find(':')
-1
>>> each_line='I tell you: today is sunday!'
>>> each_line.find(':')
10
>>>
>>> data=open('sketch.txt')
>>> for each_line in data:
if not each_line.find(':')==-1
(role, line_spoken)=each_line.split(':',1)
print(role,end='')
print(' said: ',end='')
print (line_spoken,end='')
SyntaxError: invalid syntax
>>> for each_line in data:
if not each_line.find(':')==-1:
(role, line_spoken)=each_line.split(':',1)
print(role,end='')
print(' said: ',end='')
print (line_spoken,end='')
Man said: Is this the right room for an argument?
Other Man said: I've told you once.
Man said: No you haven't!
Other Man said: Yes I have.
Man said: When?
Other Man said: Just now.
Man said: No you didn't!
Other Man said: Yes I did!
Man said: You didn't!
Other Man said: I'm telling you, I did!
Man said: You did not!
Other Man said: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man said: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man said: Just the five minutes. Thank you.
Other Man said: Anyway, I did.
Man said: You most certainly did not!
Other Man said: Now let's get one thing quite clear: I most definitely told you!
Man said: Oh no you didn't!
Other Man said: Oh yes I did!
Man said: Oh no you didn't!
Other Man said: Oh yes I did!
Man said: Oh look, this isn't an argument!
Other Man said: Yes it is!
Man said: No it isn't!
Man said: It's just contradiction!
Other Man said: No it isn't!
Man said: It IS!
Other Man said: It is NOT!
Man said: You just contradicted me!
Other Man said: No I didn't!
Man said: You DID!
Other Man said: No no no!
Man said: You did just then!
Other Man said: Nonsense!
Man said: (exasperated) Oh, this is futile!!
Other Man said: No it isn't!
Man said: Yes it is!
>>> data.close()
>>>
程序可以正常工作了,但是有些脆弱,如果文件的格式发生变化,这个代码可能会有问题,需要改变条件,代码会越来越复杂;
所以我们可以采用python的异常处理机制允许错误的出现,但监视他的发生,然后给你一个机会来恢复:
2、让错误出现,监视他的发生,然后从运行时的错误(以某种方式)恢复;
try:
#你的代码(可能导致一个运行时错误)
except:
#错误恢复代码
>>> data.close()
>>> data=open('sketch.txt')
>>> for each_line in data:
try:
(role, line_spoken)=each_line.split(':',1)
print(role,end='')
print(' said: ',end='')
print (line_spoken,end='') except:
pass Man said: Is this the right room for an argument?
Other Man said: I've told you once.
Man said: No you haven't!
Other Man said: Yes I have.
Man said: When?
Other Man said: Just now.
Man said: No you didn't!
Other Man said: Yes I did!
Man said: You didn't!
Other Man said: I'm telling you, I did!
Man said: You did not!
Other Man said: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man said: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man said: Just the five minutes. Thank you.
Other Man said: Anyway, I did.
Man said: You most certainly did not!
Other Man said: Now let's get one thing quite clear: I most definitely told you!
Man said: Oh no you didn't!
Other Man said: Oh yes I did!
Man said: Oh no you didn't!
Other Man said: Oh yes I did!
Man said: Oh look, this isn't an argument!
Other Man said: Yes it is!
Man said: No it isn't!
Man said: It's just contradiction!
Other Man said: No it isn't!
Man said: It IS!
Other Man said: It is NOT!
Man said: You just contradicted me!
Other Man said: No I didn't!
Man said: You DID!
Other Man said: No no no!
Man said: You did just then!
Other Man said: Nonsense!
Man said: (exasperated) Oh, this is futile!!
Other Man said: No it isn't!
Man said: Yes it is!
>>> data.close()
>>>
pass语句:可以认为是空语句或者Null语句,此处用来忽略捕捉到的异常,使得程序继续运行;
处理缺少的文件:将'sketch.txt'文件删除或者重命名
如果这个数据文件别删除,程序会崩溃,产生一个IOError的错误;
解决方法一:增加更多的错误检查代码
import os
if os.path.exists ('sketch.txt'):
data=open('sketch.txt')
for each_line in data:
if not each_line.find(':')==-1:
(role, line_spoken)=each_line.split(':',1)
print(role,end='')
print(' said: ',end='')
print (line_spoken,end='')
data.close()
else:
print('文件缺失')
在idle的编辑窗口中,按F5运行:
>>>
========= RESTART: D:\workspace\headfirstpython\chapter3\filemiss.py =========
文件缺失
>>>
正如所料。
解决方法二:再增加一层异常处理
try:
data=open('sketch.txt')
for each_line in data:
try:
(role, line_spoken)=each_line.split(':',1)
print(role,end='')
print(' said: ',end='')
print (line_spoken,end='')
except:
pass
data.close()
except:
print('文件缺失')
按F5运行:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
==== RESTART: D:\workspace\headfirstpython\chapter3\filemiss_tryexcept.py ====
文件缺失
>>>
正如所料。
可以发现,随着考虑的错误的增多,“增加额外的逻辑处理代码”方案复杂性也随之增加,直到最后可能掩盖程序本身的作用。
而异常处理方案就不存在该问题,python的异常处理机制可以使我们关注代码真正需要做什么,不必操心哪里会出现问题;
使用try语句使代码更易读、更易写、更容易修正!
要重点关注你的代码需要做什么!!!
但是异常处理代码太一般化,需要使用一种不那么一般化的方式使用except;
指定特定异常:在except代码行上指定错误类型。
try:
data=open('sketch.txt')
for each_line in data:
try:
(role, line_spoken)=each_line.split(':',1)
print(role,end='')
print(' said: ',end='')
print (line_spoken,end='')
except ValueError:
pass
data.close()
except IOError:
print('文件缺失')
按F5运行:
>>>
==== RESTART: D:\workspace\headfirstpython\chapter3\filemiss_tryexcept.py ====
文件缺失
>>>
ch3:文件处理与异常的更多相关文章
- MVC项目,系统找不到指定的文件。(异常来自 HRESULT:0x80070002)
今天在用Visual Studio新建MVC项目的时候,遇到错误 系统找不到指定的文件.(异常来自 HRESULT:0x80070002) 解决办法:工具--> 扩展和更新 -->联机(V ...
- SourceTree 文件被锁异常
公司用的代码管理工具是 Git 客户端用的是 SourceTree ,前些天 SourceTree 发生文件被锁异常,导致文件无法上传,下载,今天特意做个记录 异常: 解决方法:
- SpringBoot文件上传异常之提示The temporary upload location xxx is not valid
原文: 一灰灰Blog之Spring系列教程文件上传异常原理分析 SpringBoot搭建的应用,一直工作得好好的,突然发现上传文件失败,提示org.springframework.web.multi ...
- python自动化--语言基础四模块、文件读写、异常
模块1.什么是模块?可以理解为一个py文件其实就是一个模块.比如xiami.py就是一个模块,想引入使用就在代码里写import xiami即可2.模块首先从当前目录查询,如果没有再按path顺序逐一 ...
- VS 2013打开.edmx文件时报类型转换异常
供应商提交了项目代码,但在我的电脑上打开项目编译时一直报Entityframework 的 .edmx文件转换异常,而无法通过编译. 分析后认为可能是entityframework的类库不够新 ...
- Python自动化--语言基础4--模块、文件读写、异常
模块1.什么是模块?可以理解为一个py文件其实就是一个模块.比如xiami.py就是一个模块,想引入使用就在代码里写import xiami即可2.模块首先从当前目录查询,如果没有再按path顺序逐一 ...
- Python(3):文件读写与异常
访问路径: 文件读写必然涉及到文件会放在某个路径下.在python里,可以通过引入os包来实现切换当前访问的路径: # 假设我在 /home/zyq/KiDe/Python/test 文件夹中有一个文 ...
- Python 文件操作、异常
windows默认是gbk编码,又称cp936,汉字占2个字节. utf-8被称为万国码,这个编码下,汉字占3个字节. ASCII也是一种编码. 一.文件操作 最基本的文件打开: f = open(& ...
- Python文件操作,异常语法
1.文件 2.异常 1.文件的输入输出 #1.打开文件 open 函数open(file,[option])#file 是要打开的文件#option是可选择的参数,常见有 mode 等#2.文件的打 ...
随机推荐
- 多变量频率统计——r
例如有X1,X2,..,Xn个变量,我需要对每一个变量进行频次统计,如果一个一个求解的话非常麻烦,如table(X1), table(X2), ... ,table(Xn).有没有简单的语句一次性求解 ...
- 安卓程序代写 网上程序代写[原]BluetoothServerSocket详解
一. BluetoorhServerSocket简介 1. 继承关系 public final class BluetoothServerSocket extends Object implement ...
- share a story on OSPF & BGP
IP掌门把两个得意门生——BGP和OSPF叫到跟前,询问他们的修炼心得,以选择弟子传授大内心法——MPLS VPN.OSPF说:“小徒苦心研读路由原理,技术资料上万页,网络在我的围护下无环路,触发更新 ...
- 磁盘映射: between 宿主机 and 客户机
一.虚拟机映射到宿主机 在虚拟机关机的状态下,双击右侧设备栏里硬盘,在弹出的窗口中单击“实用程序“,选择“映射”.打开映射虚拟磁盘的窗口,其中的“卷”就是你希望映射虚拟机中的哪个分区到主机,如 ...
- activity 与 fragment生命周期
一.Activity的生命周期图: 二.Fragment生命周期图 三.对比图 Log数据 Activity﹕ onCreateFragment﹕ onAttachFragment﹕ onCre ...
- hibernate+spring整合增删改事务错误
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read ...
- 第三百七十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapyd部署scrapy项目
第三百七十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapyd部署scrapy项目 scrapyd模块是专门用于部署scrapy项目的,可以部署和管理scrapy项目 下载地址:h ...
- asp.net Core EF core ( Entity Framework 7 ) 数据库更新维护
CreateDatabaseIfNotExists等之前的API已经废弃,现在采用的是微软封装好,简化.高效的API,migrations 因为,旧API,要付出高昂的代价,以及局限性 打开VS20 ...
- SOA及分布式
结合领域驱动设计的SOA分布式软件架构 Windows平台分布式架构实践 - 负载均衡(下) 分享一个分布式消息总线,基于.NET Socket Tcp的发布-订阅框架,附代码下载 我终于深入参与了一 ...
- [转]Apache 监听端口失败,selinux惹的祸
原文在此 CentOS 下启动Httpd 失败,报 (13)Permission denied: make_sock: could not bind to address [::]:8000 因为 小 ...