如何从文件读入数据?

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)”语句格式不符合我们期望的格式所致。 我们发现意外情况越来越多,可以选择两种截然不同的方法:
  • 继续增加额外的逻辑对付这些异常;
  • 让错误出现,监视他的发生,然后从运行时的错误(以某种方式)恢复;
  1.  增加额外逻辑:采用字符串的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:文件处理与异常的更多相关文章

  1. MVC项目,系统找不到指定的文件。(异常来自 HRESULT:0x80070002)

    今天在用Visual Studio新建MVC项目的时候,遇到错误 系统找不到指定的文件.(异常来自 HRESULT:0x80070002) 解决办法:工具--> 扩展和更新 -->联机(V ...

  2. SourceTree 文件被锁异常

    公司用的代码管理工具是 Git 客户端用的是 SourceTree ,前些天 SourceTree 发生文件被锁异常,导致文件无法上传,下载,今天特意做个记录 异常: 解决方法:

  3. SpringBoot文件上传异常之提示The temporary upload location xxx is not valid

    原文: 一灰灰Blog之Spring系列教程文件上传异常原理分析 SpringBoot搭建的应用,一直工作得好好的,突然发现上传文件失败,提示org.springframework.web.multi ...

  4. python自动化--语言基础四模块、文件读写、异常

    模块1.什么是模块?可以理解为一个py文件其实就是一个模块.比如xiami.py就是一个模块,想引入使用就在代码里写import xiami即可2.模块首先从当前目录查询,如果没有再按path顺序逐一 ...

  5. VS 2013打开.edmx文件时报类型转换异常

      供应商提交了项目代码,但在我的电脑上打开项目编译时一直报Entityframework 的 .edmx文件转换异常,而无法通过编译.   分析后认为可能是entityframework的类库不够新 ...

  6. Python自动化--语言基础4--模块、文件读写、异常

    模块1.什么是模块?可以理解为一个py文件其实就是一个模块.比如xiami.py就是一个模块,想引入使用就在代码里写import xiami即可2.模块首先从当前目录查询,如果没有再按path顺序逐一 ...

  7. Python(3):文件读写与异常

    访问路径: 文件读写必然涉及到文件会放在某个路径下.在python里,可以通过引入os包来实现切换当前访问的路径: # 假设我在 /home/zyq/KiDe/Python/test 文件夹中有一个文 ...

  8. Python 文件操作、异常

    windows默认是gbk编码,又称cp936,汉字占2个字节. utf-8被称为万国码,这个编码下,汉字占3个字节. ASCII也是一种编码. 一.文件操作 最基本的文件打开: f = open(& ...

  9. Python文件操作,异常语法

    1.文件 2.异常 1.文件的输入输出 #1.打开文件 open 函数open(file,[option])#file 是要打开的文件#option是可选择的参数,常见有 mode 等​#2.文件的打 ...

随机推荐

  1. HBase启动后发现HMaster进程消失了

    HMaster没起来很多原因,这次看日志是这个.详细请看:http://www.bkjia.com/yjs/982064.html Hbase:namespace异常处理,hbase异常处理 Hbas ...

  2. Android 扩大view点击范围

    Android4.0设计规定的有效可触摸的UI元素标准是48dp,转化为一个物理尺寸约为9毫米.7~10毫米,这是一个用户手指能准确并且舒适触摸的区域. 如下图所示,你的UI元素可能小于48dp,图标 ...

  3. WFA 认证 启动 sigma_dut方法

    WFA认证需要启动sigma_dut,记录记录一下启动过程. Android O平台命令如下 adb shell svc wifi disable adb shell rmmod wlan adb s ...

  4. CSS的块级元素和内联元素,以及float

    说明:之前有一点搞错了,就是float其实是浮动起来,其它元素会位于它的底层. 最近在系统地学习HTML5,感觉补上了好多缺失的知识. 例如: 锚点定位其实可以通过 id 来实现: CSS 使用 !i ...

  5. 解救小哈——DFS算法举例

    一.问题引入 有一天,小哈一个人去玩迷宫.但是方向感不好的小哈很快就迷路了.小哼得知后便去解救无助的小哈.此时的小哼已经弄清楚了迷宫的地图,现在小哼要以最快的速度去解救小哈.那么,问题来了... 二. ...

  6. 使用iftop监控网卡实时流量

    Iftop工具主要用来显示本机网络流量情况及各相互通信的流量集合,如单独同哪台机器间的流量大小,非常适合于代理服务器和iptables服务器使用,这样可以方便的查看各客户端流量情况.iftop可以在类 ...

  7. EJB里的问题解答

    1.什么是EJB? EJB即Enterprise JavaBean是JavaEE应用的业务层技术标准,以这项技术开发的组件叫做EJB组件. EJB架构师一个用于开发和部署基于组件的分布式业务应用的组件 ...

  8. SOCKET,TCP/IP,UDP,HTTP,FTP总结

    一.TCP/UDP,SOCKET,HTTP,FTP简析   TCP/IP是个协议组(主要解决数据如何在网络中传输),可分为三个层次:网络层.传输层和应用层: 网络层:IP协议.ICMP协议.ARP协议 ...

  9. Java编程思想学习笔记——注解

    前言 在Android开发的过程中,我们为了减少重复代码的编写,会使用类似ButterKnife,AndroidAnnotations 这类依赖注解库.代码示例如下: //不使用 Button btn ...

  10. PGsql 基本用户权限操作

    Ⅰ. 安装与初始账户密码修改 1. 安装 sudo apt-get install postgresql-9.4 2. 管理员身份打开pg sudo -u postgres psql sudo -u ...