python学习二(文件与异常)
Python中使用open BIF与文件交互,与for语句结合使用,一次读取一行
读取文件sketch.txt,文件内容如下:
Man: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man: Just the five minutes. Thank you.
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 look, this isn't an argument!
(pause)
Other Man: Yes it is!
Man: No it isn't!
上文中说话人与所说的内容用:隔开。要求程序处理,在说话人的后面加上said,例如
Man said What's your name?
Other Man said My name is Bruce。
注意上文中的第3、7行,第3行中有两个:号,第7行没有:号
代码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='')
data.close()
split(':',1)表示数据只会根据遇到的第1个':'号分成两个部分,这样就消除了上文中第3行额外':'号的影响。
打印结果如下:红色部分为打印的ValueError异常,因为第7行没有':'号
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: 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 look, this isn't an argument!
Traceback (most recent call last):
File "E:\workspace\PythonTest\chapter3\page81.py", line 4, in <module>
(role, line_spoken) = each_line.split(':', 1)
ValueError: need more than 1 value to unpack
改写代码,处理异常
try:
data = open('sketch.txt') for each_line in data:
try:
(role, line_spoken) = each_line.split(':')
print(role, end='')
print(' said ', end='')
print(line_spoken, end='')
except ValueError:
pass data.close()
except IOError:
print('The datafile is missing!')
pass表示捕获异常后不做任务处理,正确打印结果如下
Man said Ah! (taking out his wallet and paying) Just the five minutes.
Other Man said Just the five minutes. Thank you.
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!
python学习二(文件与异常)的更多相关文章
- Python学习二:词典基础详解
作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...
- 【python学习笔记】8.异常
[python学习笔记]8.异常 raise Exception: 抛出指定异常 try/except: 捕捉异常 except: 第一个参数是需要捕获的异常类型,可以是多个类型组成元组,第二个参数是 ...
- python学习9—文件基本操作与高级操作
python学习9—文件基本操作与高级操作 1. 文件基本操作 打开文件,获得文件句柄:f = open('filename',encoding='utf-8'),open会查询操作系统的编码方式,并 ...
- python学习笔记十:异常
一.语法 #!/usr/bin/python filename='hello' #try except finally demo try: open('abc.txt') print hello ex ...
- [Python学习笔记]文件的读取写入
文件与文件路径 路径合成 os.path.join() 在Windows上,路径中以倒斜杠作为文件夹之间的分隔符,Linux或OS X中则是正斜杠.如果想要程序正确运行于所有操作系统上,就必须要处理这 ...
- Python学习--13 文件I/O
Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系 ...
- python学习总结---文件操作
# 文件操作 ### 目录管理(os) - 示例 ```python # 执行系统命令 # 清屏 # os.system('cls') # 调出计算器 # os.system('calc') # 查看 ...
- 03 python学习笔记-文件操作(三)
本文内容主要包括以下方面: 1. 文件操作基本认识2. 只读(r, rb)3. 只写(w, wb)4. 追加(a, ab)5. r+读写6. w+写读7. a+写读(追加写读)8. 文件的修改 一.文 ...
- Python学习_06_文件、IO
文件对象 python中的文件操作和c语言比较类似,包括一些缓冲.偏移量的方式. 文件对象可以通过open().file()两个内建方法创建,两个方法并没有什么不同,使用方法和c语言中的fopen() ...
随机推荐
- 代码实现跟控制器跳转到storyBoard
- Python:itertools库的使用
转于:https://blog.csdn.net/neweastsun/article/details/51965226 博主:neweastsun的专栏 介绍 itertools是python内置的 ...
- 一个有关Golang变量作用域的坑
转自:http://tonybai.com/2015/01/13/a-hole-about-variable-scope-in-golang/ 临近下班前编写和调试一段Golang代码,但运行结果始终 ...
- Spring boot 学习二:入门
1: 需要的环境: JDK:至少JDK7才支持Spring boot maven:至少3.2 spring-boot:1.2.5.RELEASE(在pom.xml中指定) 2: 创建一个maven工程 ...
- ss2
一. *** 服务端配置 1. 在命令行窗口输入下面4行命令并回车执行 yum -y update yum install -y python-setuptools && easy_i ...
- Project Server 2016 RestAPI调用测试
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- position应用之相对父元素的定位
分别添加以下style即可: 父元素position:relative; 子元素position:absolute; right:0px; bottom:0px;
- 21. Bypass D盾_防火墙(旧版 and 新版)SQL注入防御(多姿势)
D盾旧版: 00前言 D盾_IIS防火墙,目前只支持Win2003服务器,前阵子看见官方博客说D盾新版将近期推出,相信功能会更强大,这边分享一下之前的SQL注入防御的测试情况.D盾_IIS防火墙注入防 ...
- SQL笔记:中级篇
1.LIMIT 查询前多少条数据 例如:查询user表前三条数据 SELECT * FROM user LIMIT 3 ORACLE: SELECT name FROM user WHERE RO ...
- 创建、配置Servlet
1.创建Servlet 2.选择继承的类及需要覆盖的方法 3.Servlet结构 package com.sysker.servlet; import java.io.IOException; imp ...