广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python基础6
  • 151
分享到

python基础6

基础python 2023-01-31 02:01:02 151人浏览 泡泡鱼

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://www.lsjlt.com/news/185354.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python基础6
                    *******************            *  异常处理与调式         *            **********************常见错误:***1) 名字没有定义,N...
    99+
    2023-01-31
    基础 python
  • python基础-字符串(6)
    一、引言 当打来浏览器登录某些网站的时候,需要输入密码,浏览器把密码传送到服务器后,服务器会对密码进行验证,其验证过程是把之前保存的密码与本次传递过去的密码进行对比,如果相等,那么就认为密码正确,否则就认为不对;服务器既然想要存储这些密码...
    99+
    2023-01-30
    字符串 基础 python
  • 6. `Java` 并发基础之`ReentrantReadLock`
    前言:随着多线程程序的普及,线程同步的问题变得越来越常见。Java中提供了多种同步机制来确保线程安全,其中之一就是ReentrantLock。ReentrantLock是Java中比较常用的一种同...
    99+
    2023-09-21
    java 开发语言
  • Redis基础教程第6节 List
    list是一个内部采用双向链表(double linked list) 结构,像列表两端添加元素的时间复杂度为O(1)。主要功能是push、pop、获取一个范围的所有值等,操作中key理解为链表的名字。链表...
    99+
    2022-10-18
  • Python基础——1基础
    输出 print(‘把子肉爱上热干面’,‘哈哈’)  # ‘,’输出为空格 输人 name = input(‘提示的内容’) /浮点除法  %.6f //地板除法  整除 %  取余 python编码问题 (采用Unicode编码) ...
    99+
    2023-01-30
    基础 Python
  • Python基础-Python基础使用
    上篇文章 Python基础-初识Python 我们已经知道了什么是Python,Python的用处、和Python的解释器、Python的安装,这篇文章,我们主要讲Python的使用入门本文防盗链:http://python789.blog...
    99+
    2023-01-31
    基础 Python
  • Python基础篇-Python基础语法
    为什么学习pythonhttp://www.apelearn.com/bbs/thread-7739-1-1.html Python的安装 getconf LONG_BIT     查看系统版本多少位 rpm -q python uname...
    99+
    2023-01-31
    基础 语法 Python
  • Python基础--Python3基础语
    Python3 基础语法编码默认情况下,Python3源码文件以UTF-8编码,所有字符串都是Unicode字符串。当然也可以为源码文件指定不同的编码,例如:# -*- coding: cp-1252 -*-标识符1.第一个字符必须是字母表...
    99+
    2023-01-31
    基础 Python
  • Python基础
    主要是复习时总结自己不太熟悉的知识点了(面向Internet的总结)。 函数的参数 位置参数——按位置依次对应 关键字参数——按“键值对”对应 func('hello', val = 1) 调用时:若有位置参数,位置参数必须在关键字参...
    99+
    2023-01-30
    基础 Python
  • python 基础
    #列表是python最常用的数据类型,它可以作为一个方括号内的逗号分隔值出现 列表的数据类型不需要相同的类型 创建一个列表,只有在方括号([])以逗号(,)分割开即可,不需要相同的数据类型 列表表示方式 list1=['gao_wang',...
    99+
    2023-01-31
    基础 python
  • Python基础语法(Python基础知识点)
    Python与Perl,C和Java语言等有许多相似之处。不过,也有语言之间有一些明确的区别。本章的目的是让你迅速学习Python的语法。 第一个Python程序: 交互模式编程: 调用解释器不经过脚本文件...
    99+
    2022-06-04
    基础 知识点 语法
  • 【python基础】——python 复
    复数可以用使用函数 complex(real, imag) 或者是带有后缀j的浮点数来指定。比如: >>> a = complex(2, 4) >>> b = 3 - 5j >>>...
    99+
    2023-01-31
    基础 python
  • Python基础一: 计算机基础,Pyt
    1.CPU 内存 硬盘 操作系统 CPU:计算机的运算和控制中心,相当于人类的大脑。 内存:用于暂时存放CPU中的运算数据,以及与硬盘等外部存储器交换的数据。(暂时存储数据,临时加载数据及应用程序) 4G,8G,16G,32G 速度...
    99+
    2023-01-31
    基础 计算机 Python
  • Python基础之面向对象基础
    面向对象编程(Object-Oriented Programming,简称OOP)是一种编程思想,它将程序中的数据和操作封装成对象,...
    99+
    2023-09-23
    Python
  • Python--基础一
    Python基础:print & input & 变量 & 运算符 & Python数据类型 & 运算符与表达式 注释 单行注释 #我注释了一行 多行注释 三个单引号'''括起来 ''' 我...
    99+
    2023-01-30
    基础 Python
  • Python--基础二
    Python基础:字符串str & 列表list & 元组tuple & 字典dict & 集合set 字符串 str 字符串是以单引号或双引号括起来的任意文本 字符串不可变 创建字符串 str1 = ...
    99+
    2023-01-30
    基础 Python
  • Python-基础-day4
     深浅copy                          1、先看赋值运算 h1 = [1,2,3,['aihuidi','hhhh']] h2 = h1 h1[0] = 111 print(h1) print(h2) #结果:...
    99+
    2023-01-30
    基础 Python
  • Python-基础-day2
    Python环境的安装                                                                               安装Python: windows:           ...
    99+
    2023-01-30
    基础 Python
  • Python基础--webbrowser
    很多人,一提到Python,想到的就是爬虫。我会一步一步的教你如何爬出某个网站。今天就先介绍一下webbrowser,这个词您肯定不会陌生。对,就是浏览器。看看Python中对webbrowser的描述:The webbrowser mo...
    99+
    2023-01-31
    基础 Python webbrowser
  • python 基础 day3
    python 操作文件的常用方式有如下 读文件:r 模式 实例演示1:f1 = open(file='D:\Python3.5-learn\模块2\character3_文件操作\staff_table.txt',mode='r',enco...
    99+
    2023-01-31
    基础 python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作