您的位置:首页 > 编程语言 > Python开发

[Head First Python]3. 文件与异常:处理错误

2014-06-17 16:17 661 查看
datafile.txt

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? and :
(pause)
Other Man: Just now.
Man: No you didn't!


sketch.py

try:
data = open ("datafile.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('this data file is missing!')


if语句取反用 not

if not each_line.find(':') == -1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: