python学习二(文件与异常)
2013-11-23 16:40
369 查看
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
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
改写代码,处理异常
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!
读取文件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学习(二)之文件处理与错误异常
- Python学习笔记三(文件与异常)
- Python学习第五周第二次作业——文件和异常
- Python的学习笔记——文件与异常
- python学习第十章文件和异常部分课后练习自己尝试的代码
- 阿齐兹的Python学习笔记——文件与异常
- python基础学习笔记-文件与异常
- Head First Python学习笔记2——文件与异常
- Python学习笔记06-类与文件和异常
- Python学习笔记(9)-文件和异常
- Python学习系列之异常和文件操作
- Python小白白学习篇之文件与异常
- Python小白白学习篇之文件与异常
- python系列学习二——文件操作及异常处理
- Google地图发布基于python的神经网络学习模型,自动高效高准确度的从带有地理定位信息的图像文件中提取信息
- python学习—Day18—异常
- java学习笔记7 - Spring mvc 统一异常处理和静态文件的配置
- Python学习笔记之__init__.py文件的作用
- python学习二:异常处理