如何从文件读入数据?

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. ASP.NET MVC5使用AjaxHelp

    默认情况下,Visual Studio 2013新建ASP.NET MVC5项目,不包含jquery.unobtrusive-ajax.js,需要手工添加. 点击Visual Studio 2013中 ...

  2. moodle中的完形填空题的文本编写方法

    moodle中的完形填空题的文本编写方法 [完形填空题]考题把一段文字挖去一些空,让考生根据上下文正确地完成这些填空.完型填空题中的一段短文可以包括各种题目,如选择,填空,和数字题等. 题目的编辑是在 ...

  3. (原创)Linux下一定要4字节地址对齐操作

    Linux下一定要4字节地址对齐操作:“血”的教训,一定不要忘记!!! 当然不仅仅是Linux下,所有的32位机都应该如此!!!

  4. Java如何从数组中查找对象元素?

    在Java中,如何从数组中查找对象元素? 示例 以下示例使用Contains方法来搜索数组中的String对象. package com.yiibai; import java.util.*; pub ...

  5. e774. 创建JList组件

    By default, a list allows more than one item to be selected. Also, the selected items need not be co ...

  6. Unity-------------------------关于GUI绘制的编程

    转载:在这篇文章中我将给读者介绍Unity中的图形用户界面(GUI)编程.Unity有一个非常强大的GUI脚本API.它允许你使用脚本快速创建简单的菜单和GUI. 简介 Unity提供了使用脚本创建G ...

  7. (弃) Keystone CLI_可选命令详解

    本文详细介绍keystone客户端命令行界面(CLI)keystone的可选子命令.关于keystone客户端命令行工具keystone命令的子命令和选项列表,请参考前文<解读keystone命 ...

  8. C#常用数据类型间的转换

    数据类型有很多种,数据类型间的转换也是有很多的方法,如果不细心整理的话等到用的时候再查就会显得很浪费时间,所以决心整理出这篇博文.主要是讲解常用数据类型之间的转换方法以及常见数据类型所占字节数. 字节 ...

  9. 【转】Castle Windsor之组件注册

    [转]Castle Windsor之组件注册 注册方式较多,大体有这么几种,学习得比较粗浅,先记录: 1.逐个注册组件 即对每个接口通过代码指定其实现类,代码: container.Register( ...

  10. 分享9款最新超酷HTML5/CSS3应用插件

    新的一周开始了,小编继续要为大家分享实用超酷的HTML5应用,今天分享的这9款最新HTML5/CSS3应用你一定会很喜欢,一起来看看. 1.HTML5 Canvas模拟衣服撕扯动画 超级逼真 今天又要 ...