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() ...
随机推荐
- Python循环-break和continue
break用于完全结束一个循环,跳出循环体,执行循环后面的语句 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" count = ...
- linux日常管理-top动态查看负载
动态查看负载命令,具体哪个程序,哪个进程造成的系统负载. top 回车查看 3秒更新一次 第一行和uptime和w第一行显示的一样. CPU使用率,us sy 内存相关,Mem 一共多少,使用了多少, ...
- 使用JFileChooser打开文件
-----------------siwuxie095 工程名:TestFileChooser 包名:com.siwuxie095.fi ...
- Luogu 2258 [NOIP2014] 子矩阵
被普及组虐了,感觉
- 6.6 chmod的使用
从公司拷贝了白天整理的笔记,拿回家整理,结果发现有锁,无法对其解压.解决方案如上: ll 命令,查看其权限. sudo chmod 777 Picture.tar-1修改权限. 然后,可以正常打开Pc ...
- Spring入门第二十三课
我们看基于XML配置的方式配置AOP 看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int ad ...
- 【转】ANT安装、环境变量配置及验证
http://www.cnblogs.com/yuzhongwusan/archive/2013/03/26/2982411.html Posted on 2013-03-26 14:01 yuzho ...
- PostgreSQL 务实应用(四/5)JSON
JSON 可谓风靡互联网,在数据交换使用上,其优势特别明显,其结构简洁.可读易读.形式灵活.很多 API 接口的数据都采用 JSON 来表示. PostgreSQL 对 JSON 提供了良好的支持.具 ...
- sed命令使用
创建模板文件 # cat >> example.txt <<"EOF" TeSt Test test EOF 测试过程中均不使用-i参数避免模板文件内容被修 ...
- restfull知识点
网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......).因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致API ...