Python 官方文档:入门教程 => 点击学习
******************* * 异常处理与调式 * **********************常见错误:***1) 名字没有定义,N
*******************
* 异常处理与调式 *
*******************
***常见错误:***
1) 名字没有定义,NameError
In [1]: print a
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-9d7b17ad5387> in <module>()
----> 1 print a
NameError: name 'a' is not defined
2) 分母为零,ZeroDivisionError
In [2]: 10/0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<iPython-input-2-242277fd9e32> in <module>()
----> 1 10/0
ZeroDivisionError: integer division or modulo by zero
3) 文件不存在,IOError
In [3]: open("westos")
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-3-2778d2991600> in <module>()
----> 1 open("westos")
IOError: [Errno 2] No such file or directory: 'westos'
4) 语法错误,SyntaxError
In [4]: for i in [1,2,3]
File "<ipython-input-4-ae71676907af>", line 1
for i in [1,2,3]
^
SyntaxError: invalid syntax
5) 索引超出范围,IndexError
In [5]: a = [1,2,3]
In [6]: a[3]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-6-94e7916e7615> in <module>()
----> 1 a[3]
IndexError: list index out of range
In [7]: t =(1,2,3)
In [8]: t[3]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-8-7d5cf04057c5> in <module>()
----> 1 t[3]
IndexError: tuple index out of range
In [9]: t[1:9] ###切片的时候,若超出范围,则默认为全部,不报错
Out[9]: (2, 3)
####python异常处理机制:try......except......finally######
例:
#!/usr/bin/env python
#coding:utf-8
try: ###将可能发生错误的部分放在try下###
print "staring......"
li = [1,2,3]
print a
print li[3]
except IndexError: ###捕获指定的异常###
print 'index out of list length'
except NameError: ###捕获指定的异常###
print 'name is not define'
finally: ###不管是否异常,一定会执行该代码块###
print 'end......'
执行结果:
staring......
name is not define
end......
###由结果可以看出,一旦捕获到异常就不会执行下面的语句,而是到了finally,如上例,捕获到NameError后,下一条语句就不再执行,因此,并没有去捕获IndexError,结果也只是输出NameError的打印内容和finally的打印内容#####
###当没有错误的时候可以加一条条件语句,显示没有错误####
#!/usr/bin/env python
#coding:utf-8
try:
print "staring......"
li = [1,2,3]
a = 1
print a
print li[2]
except IndexError:
print 'index out of list length'
except NameError:
print 'name is not define'
else: ####如果没有异常,则执行该代码块###
print "No Error"
finally:
print 'end......'
执行结果:
staring......
1
3
No Error
end......
####可以将异常给变量,这样就可以自己打印错误####
#!/usr/bin/env python
#coding:utf-8
try:
print "staring......"
li = [1,2,3]
print a
print li[2]
except IndexError,e: ###将错误赋给e###
print e ###会自动打印错误类型###
except NameError,e:
print e
else:
print "No Error"
finally:
print 'end......'
执行结果:
staring......
name 'a' is not defined
end......
####在异常不知道的情况下,可以用BaseException,其实异常就是一个类,而BaseException是所有异常的父类####
#!/usr/bin/env python
#coding:utf-8
try:
print "staring......"
li = [1,2,3]
print li[3]
print a
except BaseException as e:
print e
except BaseException as e:
print e
else:
print "No Error"
finally:
print 'end......'
执行结果:
staring......
list index out of range
end......
####为了减少捕获异常的次数,可以将异常处理机制放在main函数下####
def func1(s):
return func2(s)*2
def func2(s):
return 10/s
def main():
try:
print func1("10")
except TypeError,e:
print e
main()
执行结果:
unsupported operand type(s) for /: 'int' and 'str'
####将错误信息写入文件###
import logging ###导入logging模块###
logging.basicConfig(filename='err.log') ###使用basicConfig方法###
def func1(s):
return func2(s)*2
def func2(s):
return 10/s
def main():
try:
print func1("10")
except Exception as e:
logging.exception(e) ###将异常信息写入err.log文件###
main()
err.log文件的内容:
ERROR:root:unsupported operand type(s) for /: 'int' and 'str'
Traceback (most recent call last):
File "/home/kiOSk/PyCharmProjects/pythonbasic/py5.1/error.py", line 48, in main
print func1("10")
File "/home/kiosk/PycharmProjects/pythonbasic/py5.1/error.py", line 43, in func1
return func2(s)*2
File "/home/kiosk/PycharmProjects/pythonbasic/py5.1/error.py", line 45, in func2
return 10/s
TypeError: unsupported operand type(s) for /: 'int' and 'str'
####由此可见,以将错误的信息导入到了err.log文件###
####抛出异常和自定义异常###
a = 1 ###抛出异常###
if a == 1:
raise NameError
执行结果:
Traceback (most recent call last):
File "/home/kiosk/PycharmProjects/pythonbasic/py5.1/error.py", line 57, in <module>
raise NameError
NameError
class MyError(BaseException): ###自定义异常###
pass
a = 1
if a == 1:
raise MyError
执行结果:
charmProjects/pythonbasic/py5.1/error.py
Traceback (most recent call last):
File "/home/kiosk/PycharmProjects/pythonbasic/py5.1/error.py", line 60, in <module>
raise MyError
__main__.MyError
注意:一定不能够即捕获异常有抛出异常
#####调试-断言####
断言失败:
def foo(s):
n = int(s)
return 10 / n
def main():
foo('0')
assert foo(5) == 1 ###断言失败,assert 语句本身抛出AssertionError###
print 'hello'
执行结果:
Traceback (most recent call last):
File "/home/kiosk/PycharmProjects/pythonbasic/py5.1/error.py", line 68, in <module>
assert foo(5) == 1
AssertionError
断言成功:
def foo(s):
n = int(s)
return 10 / n
def main():
foo('0')
assert foo(5) == 2 ###断言成功,就执行下面的语句###
print 'hello'
执行结果:
hello
Python 解释器执行时可以用 -O 参数来关闭 assert,把所有的 assert 语句
当成 pass
例:
[kiosk@foundation38 py5.1]$ python -O error.py
hello
####将错误记录到文件里###
import logging
logging.basicConfig(filename='logging.log',level=logging.WARNING) ###level=logging.WARNING定义日志级别为WARNING
def foo(s):
n = int(s)
logging.info('n=%d'% n) ###日志级别为info的内容###
logging.warning('n=%d...warn' % n)
return 10 / n
def main():
foo('0')
main()
执行后logging.log的内容为:
WARNING:root:n=0...warn ###由此可见,并没有将日志级别为info的内容写入文件,因为,在最开始已经定义了级别为WARNING,除非级别比WARNING大的会记录到文件里###
例如:一开始定义日志级别为info
import logging
logging.basicConfig(filename='logging.log',level=logging.INFO)
def foo(s):
n = int(s)
logging.info('n=%d'% n)
logging.warning('n=%d...warn' % n)
return 10 / n
def main():
foo('0')
main()
则执行后logging.log的内容为:
INFO:root:n=0
WARNING:root:n=0...warn
###日志级别:debug,info,warning,error####
###调试-pdb#####
pdb让程序以单步方式运行,随时查看运行状态。n 可以单步执行代码,p 变量名 来查看变量,q 结束调试,退出程序
[kiosk@foundation38 py5.1]$ python -m pdb error.py
> /home/kiosk/PycharmProjects/pythonbasic/py5.1/error.py(3)<module>()
-> _author_ = "xiao"
(Pdb) n
> /home/kiosk/PycharmProjects/pythonbasic/py5.1/error.py(64)<module>()
-> def foo(s):
(Pdb) n
> /home/kiosk/PycharmProjects/pythonbasic/py5.1/error.py(69)<module>()
-> def main():
(Pdb)
####pdb.set_trace()####
import pdb
n1 = 1
n = int(n1)
print n
pdb.set_trace()
print 'world'
pdb.set_trace()
print 'hello'
s = 2
print s
调试:
1
> /home/kiosk/PycharmProjects/pythonbasic/py5.1/error.py(81)<module>()
-> print 'world'
(Pdb) n
world
> /home/kiosk/PycharmProjects/pythonbasic/py5.1/error.py(82)<module>()
-> pdb.set_trace()
(Pdb) c ###继续###
> /home/kiosk/PycharmProjects/pythonbasic/py5.1/error.py(83)<module>()
-> print 'hello'
(Pdb) c
hello
2
*****************
* 正则表达式 *
*****************
###re.findall(p,text)###
将能匹配上的全返回,会返回一个 list
In [13]: s = 'redhat linux hello world'
In [14]: r = 'linux'
In [15]: import re ###导入re模块###
In [16]: re.findall(r,s) ###如果r在s里,则返回###
Out[16]: ['linux']
In [17]: re.findall('red',s)
Out[17]: ['red']
In [18]: re.findall('westos',s) ###如果不在,则返回空###
Out[18]: []
In [1]: s = 'python linux hello wor\\ld'
In [2]: r = 'wor\\l'
In [3]: import re
In [4]: re.findall(r,s) ###因为\\是特殊字符,会认为是一个\,所以会找不到###
Out[4]: []
In [5]: r1 = r'wor\\l' ###在前面加上r,就不会认为是特殊字符,就可以找到####
In [6]: re.findall(r1,s)
Out[6]: ['wor\\l']
In [7]:
####基本模式####
1 字面模式: 就是字面长量,就代表其本身
2 . :匹配任何字符
3 \d:匹配任何十进制数
4 \D:匹配任何非数字字符
5 \s:匹配任何空白字符
6 \S:匹配任何非空间字符
7 \w:匹配任何字母数字字符
8 \W:匹配任何非字母数字自符
9 ^ 开头 $ 结尾
10 \ 转义字符
###次数的匹配###
次数的匹配 , 匹配其前面的字符出现的次数 :
* 0 次或多次
+ 一次或多次
? 零次或一次
{n} 出现 n 次
{m,n} 出现 m 到 n 次 :{0,}相当于*,{1,}相当于+,{0,1}匹配一次或零次,相当于?
练习:
1 判断一个字符串是否是合法的 Email 的方法(要求以.com结尾的为合法的)
In [16]: r = r'\w+@\w+\.com'
In [17]: import re
In [18]: re.findall(r,'hello@westos.org fentiao@westos.com')
Out[18]: ['fentiao@westos.com']
2 判断满足029-1234567这样要求的电话号码的方法
In [19]: r = r'^\d{3}-\d{7}'
In [20]: re.findall(r,'029-1234567 1234-1234567')
Out[20]: ['029-1234567']
3 判断变量是否合法
In [30]: r = r'^[_a-zA-Z]\w*$'
In [31]: re.findall(r,'_a-1')
Out[31]: []
In [32]: re.findall(r,'a')
Out[32]: ['a']
In [33]: re.findall(r,'a1')
Out[33]: ['a1']
In [34]: re.findall(r,'_a1')
Out[34]: ['_a1']
In [35]: re.findall(r,'1a1')
Out[35]: []
####编译re.compile()###
当我们在 Python 中使用正则表达式时,re 模块内部会干两件事情:
1. 编译正则表达式,如果正则表达式的字符串本身不合法,会报错;
2. 用编译后的正则表达式去匹配字符串。
重复使用几千次,出于效率的考虑,我们可以预编译该正则表达式。
In [36]: r = r't.p'
In [37]: p = re.compile(r)
In [38]: print p
<_sre.SRE_Pattern object at 0x1f6a3c0>
In [39]: re.findall(p,'top tap tab')
Out[39]: ['top', 'tap']
###re.match(p,text)###
re.match(p,text) :p 为正则表达式模式, text 要查找的字符串,会返回一个match 对象
一定要用group()查看返回值
In [40]: re.match(p,'top tap tab')
Out[40]: <_sre.SRE_Match at 0x23D0850>
In [41]: a = re.match(p,'top tap tab')
In [42]: a.group() ###显示匹配到的第一个字符,即在‘top tap tab’中查看第一个是否满足p的条件,满足则匹配成功返回,不满足则报错###
Out[42]: 'top'
In [43]: a.start() ###显示匹配的开始###
Out[43]: 0
In [44]: a.end() ###显示匹配的结束###
Out[44]: 3
In [46]: a.span() ###显示匹配的全部长度###
Out[46]: (0, 3)
####当字符串中的第一项不满足匹配条件时,报错####
In [16]: a = re.match(p,'tab top tap')
In [17]: a.group()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-17-af218c045ead> in <module>()
----> 1 a.group()
AttributeError: 'NoneType' object has no attribute 'group'
###一般用条件判断语句来描述是否匹配到,如果字符串的第一个字符是所匹配的,则打印找到匹配,如果不是,则打印没有匹配###
In [54]: if a:
....: print 'Match found:%s'% a.group()
....: else:
....: print 'no match'
....:
no match
###re.search(p,text)######
只要在 text 中匹配到了 p 就返回,只返回第一个匹配到的,与re.match(p,text)的区别在与re.match(p,text)只在text的第一个查看是否匹配到p,而re.search(p,text)是在全部text中查找,只要text中有匹配到p的就返回
In [33]: p
Out[33]: re.compile(r'hello')
In [34]: re.search(p,'westos redhat hello linux')
Out[34]: <_sre.SRE_Match at 0x2033f38>
In [35]: a = re.search(p,'westos redhat hello linux') ###hello并不在第一个,但是仍然匹配到,并且返回了####
In [36]: a.group()
Out[36]: 'hello'
In [37]:
小练习:要求以.com或者.cn结尾的为合法的邮件,判断字符串是否为合法的邮件
In [32]: email = r'\w+@\w+(\.com|\.cn)'
In [33]: a = re.search(email,'hello@example.org hello@example.com')
In [34]: a.group()
Out[34]: 'hello@example.com'
####re.finditer(p,text)####
找到re匹配的所有子串,并把它们作为一个迭代器返回
In [1]: import re
In [2]: a = re.fi
re.findall re.finditer
In [2]: a = re.finditer(r'hello', 'hello westos')
In [3]: a.next().group()
Out[3]: 'hello'
###re.sub(p,s,text)###
替换,将 p 匹配到的字符替换为 s
In [4]: s = 'hello westos'
In [6]: re.sub(r'wes..s','world','hello westos') ###第一个字符指被替换的字符,第二个字符指要替换成的字符,第三个字符指被替换的字符串###
Out[6]: 'hello world'
In [7]: re.subn(r'wes..s','world','hello westos') ###subn显示替换的次数###
Out[7]: ('hello world', 1)
In [8]: re.subn(r'wes..s','world','hello westos westos')
Out[8]: ('hello world world', 2)
####re.split(p,text)###
按照 p 匹配,并且以匹配到的字符为分隔符切割 text, 返回一个切割后的 list
In [10]: re.split(r'[\+\*]','123+34*18') ###以+和*作为分割,将'123+34*18'切割###
Out[10]: ['123', '34', '18']
小练习:从Http://172.25.254.252下提取图片
#!/usr/bin/env python
#coding:utf-8
import re
import urllib,urllib2
def gethtml(url):
try:
page = urllib.urlopen(url) ###打开网址###
html = page.read() ###读出网址的内容###
except urllib2.URLError,e:
print 'Download error...%s'% e
return html
def getImg(html):
img_re = r'src="(.+\.jpg)"'
img_recom = re.compile(img_re)
imglist = re.findall(img_recom,html)
x = 1
for imgurl in imglist:
urllib.urlretrieve(imgurl,"%s.png" %x)
x += 1
html = getHtml('http://172.25.254.252/')
getImg(html)
--结束END--
本文标题: python基础6
本文链接: https://lsjlt.com/news/185354.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0