如何从文件读入数据?

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. R语言如何将字符串转变为命令执行

    这里用到 eval() 和 parse() 函数.首先使用 parse() 函数将字符串转化为表达式(expression),而后使用 eval() 函数对表达式求解.x <- 1:10a &l ...

  2. snapshots On Vmware

    快照使用 编辑 如果你创建了多于一个的虚拟机快照,那么,你将有多个还原点可以用于恢复.当你创建了一个快照,那快照些现在可写的在那个点上就变成了只读的.使用in-file delta技术就能创建新文件记 ...

  3. 自然语言交流系统 phxnet团队 创新实训 项目博客 (五)

    3DMax方面所涉及的专业知识:                       (1)一下的关于3DMax中对于人物的设计和操作均需要在对3DMax基础知识熟练掌握的情况下进行的. (2)骨骼架设:首先 ...

  4. Playing FPS Games with Deep Reinforcement Learning

    论文不同点: (1)用两套网络分别实现移动和射击. (2)使用LSTM来处理不完全信息. 疑问: (1)为什么对于射击使用RNN,对导航却没有使用RNN.一般来说,当我们看见视野里面有敌人的时候,我们 ...

  5. Maven外部依赖

    正如大家所了解的那样,Maven确实使用 Maven 库的概念作依赖管理.但是,如果依赖是在远程存储库和中央存储库不提供那会怎么样? Maven 提供为使用外部依赖的概念,就是应用在这样的场景中的. ...

  6. 使用Maven构建和测试Java项目

    我们在创建项目时要学习的是如何使用 Maven 来创建一个 Java 应用程序.现在将学习如何构建和测试应用程序. 进入到 C:\MVN 目录我们准备创建来 java应用程序.打开 consumerB ...

  7. LintCode #2 尾部的零

    计算阶乘尾部的0的个数,初一看很简单. 先上代码 public static long GetFactorial(long n) { || n == ) ; ); } //Main方法中调用 ); ; ...

  8. vs2015配置mysql数据库时,mysql.data、mysql.data.entity、EntityFramework的安装错误问题

    vs2015连接mysql数据库常见问题 最近在vs2015用asp.net开发一个网站,要连接mysql数据库,于是百度了一下相关配置的文章,有好几篇文章说了相关步骤,但是我装的时候还是遇到了问题, ...

  9. Ubuntu 14.04 安装 DevStack与遇到的的问题记录

    本文总结Ubuntu 14.04下部署DevStack的过程以及一些可能遇到的问题. 一.安装 以下的操作最好在普通用户下进行,至少在git clone devstack的时候使用普通用户,这样可以避 ...

  10. Hadoop(HA)分布式集群部署

    Hadoop(HA)分布式集群部署和单节点namenode部署其实一样,只是配置文件的不同罢了. 这篇就讲解hadoop双namenode的部署,实现高可用. 系统环境: OS: CentOS 6.8 ...