Python编程从入门到实践笔记——异常和存储数据
<span style="color: #008000;">#
<span style="color: #008000;">1.处理ZeroDivisionError异常<span style="color: #008000;"> <span style="color: #008000;">print(5/0)<span style="color: #008000;">#
<span style="color: #008000;">2.使用try-except 代码块<span style="color: #0000ff;">try<span style="color: #000000;">:
<span style="color: #0000ff;">print(5/<span style="color: #000000;">0)
<span style="color: #0000ff;">except<span style="color: #000000;"> ZeroDivisionError:
<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">You can't devide by zero.<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #008000;">#<span style="color: #008000;">3.使用异常避免崩溃
<span style="color: #008000;">#<span style="color: #008000;">4.else 代码块<span style="color: #008000;">
<span style="color: #008000;">依赖于try 代码块成功执行的代码都应放到else 代码块中:
<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">Give me two numbers,and I'll divide them.<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">Enter 'q' to quit.<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">while<span style="color: #000000;"> True:
first_number = input(<span style="color: #800000;">"<span style="color: #800000;">\nFirst number: <span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">if first_number == <span style="color: #800000;">'<span style="color: #800000;">q<span style="color: #800000;">'<span style="color: #000000;">:
<span style="color: #0000ff;">break<span style="color: #000000;">
second_number = input(<span style="color: #800000;">"<span style="color: #800000;">Second number: <span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">try<span style="color: #000000;">:
answer = int(first_number) /<span style="color: #000000;"> int(second_number)
<span style="color: #0000ff;">except<span style="color: #000000;"> ZeroDivisionError:
<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">You can't divide by 0!<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">print<span style="color: #000000;">(answer)
<span style="color: #008000;">#<span style="color: #008000;">5.处理FileNotFoundError异常
filename = <span style="color: #800000;">'<span style="color: #800000;">alice.txt<span style="color: #800000;">'
<span style="color: #0000ff;">try<span style="color: #000000;">:
with open(filename) as f_obj:
contents =<span style="color: #000000;"> f_obj.read()
<span style="color: #0000ff;">except<span style="color: #000000;"> FileNotFoundError:
msg = <span style="color: #800000;">"<span style="color: #800000;">Sorry,the file <span style="color: #800000;">" + filename + <span style="color: #800000;">"<span style="color: #800000;"> does not exist.<span style="color: #800000;">"
<span style="color: #0000ff;">print<span style="color: #000000;">(msg)
<span style="color: #008000;">#<span style="color: #008000;">6.分析文本
filename = <span style="color: #800000;">'<span style="color: #800000;">alice.txt<span style="color: #800000;">'
<span style="color: #0000ff;">try<span style="color: #000000;">:
with open(filename) as f_obj:
contents =<span style="color: #000000;"> f_obj.read()
<span style="color: #0000ff;">except<span style="color: #000000;"> FileNotFoundError:
msg = <span style="color: #800000;">"<span style="color: #800000;">Sorry,the file <span style="color: #800000;">" + filename + <span style="color: #800000;">"<span style="color: #800000;"> does not exist.<span style="color: #800000;">"
<span style="color: #0000ff;">print<span style="color: #000000;">(msg)
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #008000;">#<span style="color: #008000;"> 计算文件大致包含多少个单词
words =<span style="color: #000000;"> contents.split()
num_words =<span style="color: #000000;"> len(words)
<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">The file <span style="color: #800000;">" + filename + <span style="color: #800000;">"<span style="color: #800000;"> has about <span style="color: #800000;">" + str(num_words) + <span style="color: #800000;">"<span style="color: #800000;"> words.<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #008000;">#<span style="color: #008000;">7.使用多个文件
<span style="color: #0000ff;">def<span style="color: #000000;"> count_words(filename):
<span style="color: #800000;">"""<span style="color: #800000;">--snip--<span style="color: #800000;">"""<span style="color: #000000;">
filenames = [<span style="color: #800000;">'<span style="color: #800000;">alice.txt<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">siddhartha.txt<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">moby_dick.txt<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">little_women.txt<span style="color: #800000;">'<span style="color: #000000;">]
<span style="color: #0000ff;">for filename <span style="color: #0000ff;">in<span style="color: #000000;"> filenames:
count_words(filename)
<span style="color: #008000;">#<span style="color: #008000;">8.失败时一声不吭 pass语句
<span style="color: #0000ff;">def<span style="color: #000000;"> count_words(filename):
<span style="color: #800000;">"""<span style="color: #800000;">计算一个文件大致包含多少个单词<span style="color: #800000;">"""
<span style="color: #0000ff;">try<span style="color: #000000;">:
<span style="color: #800000;">"""<span style="color: #800000;">--snip--<span style="color: #800000;">"""
<span style="color: #0000ff;">except<span style="color: #000000;"> FileNotFoundError:
<span style="color: #0000ff;">pass
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #800000;">"""<span style="color: #800000;">--snip--<span style="color: #800000;">"""
<span style="color: #008000;">#<span style="color: #008000;">10.4存储数据<span style="color: #008000;">
<span style="color: #008000;">1.使用json.dump() 和json.load()
<span style="color: #0000ff;">import<span style="color: #000000;"> json
numbers = [2,3,5,7,11,13<span style="color: #000000;">]
filename = <span style="color: #800000;">'<span style="color: #800000;">numbers.json<span style="color: #800000;">'<span style="color: #000000;">
with open(filename,<span style="color: #800000;">'<span style="color: #800000;">w<span style="color: #800000;">'<span style="color: #000000;">) as f_obj:
json.dump(numbers,f_obj)
<span style="color: #008000;">#<span style="color: #008000;">2.保存和读取用户生成的数据
<span style="color: #0000ff;">import<span style="color: #000000;"> json
username = input(<span style="color: #800000;">"<span style="color: #800000;">What is your name? <span style="color: #800000;">"<span style="color: #000000;">)
filename = <span style="color: #800000;">'<span style="color: #800000;">username.json<span style="color: #800000;">'<span style="color: #000000;">
with open(filename,<span style="color: #800000;">'<span style="color: #800000;">w<span style="color: #800000;">'<span style="color: #000000;">) as f_obj:
json.dump(username,f_obj)
<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">We'll remember you when you come back,<span style="color: #800000;">" + username + <span style="color: #800000;">"<span style="color: #800000;">!<span style="color: #800000;">"<span style="color: #000000;">)
with open(filename) as f_obj:
username =<span style="color: #000000;"> json.load(f_obj)
<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">Welcome back,<span style="color: #800000;">" + username + <span style="color: #800000;">"<span style="color: #800000;">!<span style="color: #800000;">")