前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python基础学习笔记之(二)(华工大神)

Python基础学习笔记之(二)(华工大神)

作者头像
bear_fish
发布2018-09-20 15:44:32
4580
发布2018-09-20 15:44:32
举报

Python基础学习笔记之(二)

zouxy09@qq.com

http://blog.csdn.net/zouxy09

六、包与模块

1、模块module

         Python中每一个.py脚本定义一个模块,所以我们可以在一个.py脚本中定义一个实现某个功能的函数或者脚本,这样其他的.py脚本就可以调用这个模块了。调用的方式有三种,如下:

[python] view plaincopy

  1. ###################################
  2. ## package and module ####
  3. ## a .py file define a module which can be used in other script
  4. ## as a script, the name of module is the same as the name of the .py file
  5. ## and we use the name to import to a new script
  6. ## e.g., items.py, import items
  7. ## python contains many .py files, which we can import and use
  8. # vi cal.py
  9. def add(x, y):  
  10. return x + y  
  11. def sub(x, y):  
  12. return x - y  
  13. def mul(x, y):  
  14. return x * y  
  15. def div(x, y):  
  16. return x / y  
  17. print "Your answer is: ", add(3, 5)  
  18. if __name__ == "__main__"
  19.     r = add(1, 3)  
  20. print r  
  21. # vi test.py
  22. import cal # will expand cal.py here
  23. # so, this will execute the following code in cal.py
  24. # print "Your answer is: ", add(3, 5)
  25. # it will print "Your answer is: 8"
  26. # but as we import cal.py, we just want to use those functions
  27. # so the above code can do this for me, the r=add(1, 3) will not execute
  28. result = cal.add(1, 2)  
  29. print result  
  30. # or
  31. import cal as c  
  32. result = c.add(1, 2)  
  33. # or
  34. from cal import add  
  35. result = add(1, 2)  

2、包package

       python 的每个.py文件执行某种功能,那有时候我们需要多个.py完成某个更大的功能,或者我们需要将同类功能的.py文件组织到一个地方,这样就可以很方便我们的使用。模块可以按目录组织为包,创建一个包的步骤:

# 1、建立一个名字为包名字的文件夹

# 2、在该文件夹下创建一个__init__.py空文件

# 3、根据需要在该文件夹下存放.py脚本文件、已编译拓展及子包

# 4、import pack.m1,pack.m2 pack.m3

[python] view plaincopy

  1. #### package 包
  2. ## python 的模块可以按目录组织为包,创建一个包的步骤:
  3. # 1、建立一个名字为包名字的文件夹
  4. # 2、在该文件夹下创建一个__init__.py 空文件
  5. # 3、根据需要在该文件夹下存放.py脚本文件、已编译拓展及子包
  6. # 4、import pack.m1, pack.m2 pack.m3
  7. mkdir calSet  
  8. cd calSet  
  9. touch __init_.py  
  10. cp cal.py .  
  11. # vi test.py
  12. import calSet.cal  
  13. result = calSet.cal.add(1, 2)  
  14. print result  

七、正则表达式

       正则表达式,(英语:RegularExpression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些符合某个模式的文本。

         Python提供了功能强大的正则表达式引擎re,我们可以利用这个模块来利用正则表达式进行字符串操作。我们用import re来导入这个模块。

         正则表达式包含了很多规则,如果能灵活的使用,在匹配字符串方面是非常高效率的。更多的规则,我们需要查阅其他的资料。

1、元字符

         很多,一些常用的元字符的使用方法如下:

[python] view plaincopy

  1. ##############################
  2. ## 正则表达式 RE
  3. ## re module in python
  4. import re  
  5. rule = r'abc' # r prefix, the rule you want to check in a given string
  6. re.findall(rule, "aaaaabcaaaaaabcaa") # return ['abc', 'abc']
  7. # [] 用来指定一个字符集 [abc] 表示 abc其中任意一个字符符合都可以
  8. rule = r"t[io]p"
  9. re.findall(rule, "tip tep twp top") # return ['tip', 'top']
  10. # ^ 表示 补集,例如[^io] 表示除i和o外的其他字符
  11. rule = r"t[^io]p"
  12. re.findall(rule, "tip tep twp top") # return ['tep', 'twp']
  13. # ^ 也可以 匹配行首,表示要在行首才匹配,其他地方不匹配
  14. rule = r"^hello"
  15. re.findall(rule, "hello tep twp hello") # return ['hello']
  16. re.findall(rule, "tep twp hello") # return []
  17. # $ 表示匹配行尾
  18. rule = r"hello$"
  19. re.findall(rule, "hello tep twp hello") # return ['hello']
  20. re.findall(rule, "hello tep twp") # return []
  21. # - 表示范围
  22. rule = r"x[0123456789]x" # the same as
  23. rule = r"x[0-9]x"
  24. re.findall(rule, "x1x x4x xxx") # return ['x1x', 'x4x']
  25. rule = r"x[a-zA-Z]x"
  26. # \ 表示转义符
  27. rule = r"\^hello"
  28. re.findall(rule, "hello twp ^hello") # return ['^hello']
  29. # \d 匹配一个数字字符。等价于[0-9]。
  30. # \D 匹配一个非数字字符。等价于[^0-9]。
  31. # \n 匹配一个换行符。等价于\x0a和\cJ。
  32. # \r 匹配一个回车符。等价于\x0d和\cM。
  33. # \s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
  34. # \S 匹配任何非空白字符。等价于[^ \f\n\r\t\v]。
  35. # \t 匹配一个制表符。等价于\x09和\cI。
  36. # \w 匹配包括下划线的任何单词字符。等价于“[A-Za-z0-9_]”。
  37. # \W 匹配任何非单词字符。等价于“[^A-Za-z0-9_]”。
  38. # {} 表示重复规则
  39. # 例如我们要查找匹配是否是 广州的号码,020-八位数据
  40. # 以下三种方式都可以实现
  41. rule = r"^020-\d\d\d\d\d\d\d\d$"
  42. rule = r"^020-\d{8}$" # {8} 表示前面的规则重复8次
  43. rule = r"^020-[0-9]{8}$"
  44. re.findall(rule, "020-23546813") # return ['020-23546813']
  45. # * 表示将其前面的字符重复0或者多次
  46. rule = r"ab*"
  47. re.findall(rule, "a") # return ['a']
  48. re.findall(rule, "ab") # return ['ab']
  49. # + 表示将其前面的字符重复1或者多次
  50. rule = r"ab+"
  51. re.findall(rule, "a") # return []
  52. re.findall(rule, "ab") # return ['ab']
  53. re.findall(rule, "abb") # return ['abb']
  54. # ? 表示前面的字符可有可无
  55. rule = r"^020-?\d{8}$"
  56. re.findall(rule, "02023546813") # return ['020-23546813
  57. re.findall(rule, "020-23546813") # return ['020-23546813']
  58. re.findall(rule, "020--23546813") # return []
  59. # ? 表示非贪婪匹配
  60. rule = r"ab+?"
  61. re.findall(rule, "abbbbbbb") # return ['ab']
  62. # {} 可以表示范围
  63. rule = r"a{1,3}"
  64. re.findall(rule, "a") # return ['a']
  65. re.findall(rule, "aa") # return ['aa']
  66. re.findall(rule, "aaa") # return ['aaa']
  67. re.findall(rule, "aaaa") # return ['aaa', 'a']
  68. ## compile re string
  69. rule = r"\d{3,4}-?\d{8}"
  70. re.findall(rule, "020-23546813")  
  71. # faster when you compile it
  72. # return a object
  73. p_tel = re.compile(rule)  
  74. p_tel.findall("020-23546813")  
  75. # the parameter re.I 不区分大小写
  76. name_re = re.compile(r"xiaoyi", re.I)  
  77. name_re.findall("Xiaoyi")  
  78. name_re.findall("XiaoYi")  
  79. name_re.findall("xiAOyi")  

2、常用函数

         Re模块作为一个对象,它还支持很多的操作,例如:

[python] view plaincopy

  1. # the object contain some methods we can use
  2. # match 去搜索字符串开头,如果匹配对,那就返回一个对象,否则返回空
  3. obj = name_re.match('Xiaoyi, Zou')  
  4. # search 去搜索字符串(任何位置),如果匹配对,那就返回一个对象
  5. obj = name_re.search('Zou, Xiaoyi')  
  6. # 然后可以用它来进行判断某字符串是否存在我们的正则表达式
  7. if obj:  
  8. pass
  9. # findall 返回一个满足正则的列表
  10. name_re.findall("Xiaoyi")  
  11. # finditer 返回一个满足正则的迭代器
  12. name_re.finditer("Xiaoyi")  
  13. # 正则替换
  14. rs = r"z..x"
  15. re.sub(rs, 'python', 'zoux ni ziox me') # return 'python ni python me'
  16. re.subn(rs, 'python', 'zoux ni ziox me') # return ('python ni python me', 2), contain a number
  17. # 正则切片
  18. str = "123+345-32*78"
  19. re.split(r'[\+\-\*]', str) # return ['123', '345', '32', '78']
  20. # 可以打印re模块支持的属性和方法,然后用help
  21. dir(re)  
  22. ##### 编译正则表达式式 可以加入一些属性,可以增加很多功能
  23. # 多行匹配
  24. str = """
  25.     hello xiaoyi
  26.     xiaoyi hello
  27.     hello zou
  28.     xiaoyi hello
  29.     """
  30. re.findall(r'xiaoyi', str, re.M)  

3、分组

         分组有两个作用,它用()来定义一个组,组内的规则只对组内有效。

[python] view plaincopy

  1. # () 分组
  2. email = r"\w{3}@\w+(\.com|\.cn|\.org)"
  3. re.match(email, "zzz@scut.com")  
  4. re.match(email, "zzz@scut.cn")  
  5. re.match(email, "zzz@scut.org")  

    另外,分组可以优先返回分组内匹配的字符串。

[python] view plaincopy

  1. # 另外,分组可以优先返回分组内匹配的字符串
  2. str = """
  3.     idk hello name=zou yes ok d
  4.     hello name=xiaoyi yes no dksl
  5.     dfi lkasf dfkdf hello name=zouxy yes d
  6.     """
  7. r1 = r"hello name=.+ yes"
  8. re.findall(r1, str) # return ['hello name=zou yes', 'hello name=xiaoyi yes', 'hello name=zouxy yes']
  9. r2 = r"hello name=(.+) yes"
  10. re.findall(r2, str) # return ['zou', 'xiaoyi', 'zouxy']
  11. # 可以看到,它会匹配整个正则表达式,但只会返回()括号分组内的字符串,
  12. # 用这个属性,我们就可以进行爬虫,抓取一些想要的数据

4、一个小实例-爬虫

         这个实例利用上面的正则和分组的优先返回特性来实现一个小爬虫算法。它的功能是到一个给定的网址里面将.jpg后缀的图片全部下载下来。

[python] view plaincopy

  1. ## 一个小爬虫
  2. ## 下载贴吧 或 空间中的所有图片
  3. ## getJpg.py
  4. #!/usr/bin/python
  5. import re  
  6. import urllib  
  7. # Get the source code of a website
  8. def getHtml(url):  
  9. print 'Getting html source code...'
  10.     page = urllib.open(url)  
  11.     html = page.read()  
  12. return html  
  13. # Open the website and check up the address of images,
  14. # and find the common features to decide the re_rule
  15. def getImageAddrList(html):  
  16. print 'Getting all address of images...'
  17.     rule = r"src=\"(.+\.jpg)\" pic_ext"
  18.     imReg = re.compile(rule)  
  19.     imList = re.findall(imReg, html)  
  20. return imList  
  21. def getImage(imList):  
  22. print 'Downloading...'
  23.     name = 1;  
  24. for imgurl in imList:  
  25.         urllib.urlretrieve(imgurl, '%s.jpg' % name)  
  26.         name += 1
  27. print 'Got ', len(imList), ' images!'
  28. ## main
  29. htmlAddr = "http://tieba.baidu.com/p/2510089409"
  30. html = getHtml(htmlAddr)  
  31. imList = getImageAddrList(html)  
  32. getImage(imList)  

八、深拷贝与浅拷贝

Python中对数据的复制有两个需要注意的差别:

浅拷贝:对引用对象的拷贝(只拷贝父对象),深拷贝:对对象资源的拷贝。具体的差别如下:

[python] view plaincopy

  1. ##############################
  2. ### memory operation
  3. ## 浅拷贝:对引用对象的拷贝(只拷贝父对象)
  4. ## 深拷贝:对对象资源的拷贝
  5. a = [1, 2, 3]  
  6. b = a # id(a) == id (b), 同一个标签,相当于引用
  7. a.append(4) # a = [1, 2, 3, 4], and b also change to = [1, 2, 3, 4]
  8. import copy  
  9. a = [1, 2, ['a', 'b']] # 二元列表
  10. c = copy.copy(a)  # id(c) != id(a)
  11. a.append('d') # a = [1, 2, ['a', 'b'], 'd'] but c keeps not changed
  12. # 但只属于浅拷贝,只拷贝父对象
  13. # 所以 id(a[0]) == id(c[0]),也就是说对a追加的元素不影响c,
  14. # 但修改a被拷贝的数据后,c的对应数据也会改变,因为拷贝不会改变元素的地址
  15. a[2].append('d') # will change c, too
  16. a[1] = 3 # will change c, too
  17. # 深拷贝
  18. d = copy.deepcopy(a) # 全部拷贝,至此恩断义绝,两者各走
  19. # 各的阳关道和独木桥,以后毫无瓜葛

九、文件与目录

1、文件读写

        Python的文件操作和其他的语言没有太大的差别。通过open或者file类来访问。但python支持了很多的方法,以支持文件内容和list等类型的交互。具体如下:

[python] view plaincopy

  1. ########################
  2. ## file and directory
  3. # file_handler = open(filename, mode)
  4. # mode is the same as other program langurage
  5. ## read
  6. # method 1
  7. fin = open('./test.txt')  
  8. fin.read()  
  9. fin.close()  
  10. # method 2, class file
  11. fin = file('./test.txt')  
  12. fin.read()  
  13. fin.close()  
  14. ## write
  15. fin = open('./test.txt', 'r+') # r, r+, w, w+, a, a+, b, U
  16. fin.write('hello')  
  17. fin.close()  
  18. ### 文件对象的方法
  19. ## help(file)
  20. for i in open('test.txt'):  
  21. print i  
  22. str = fin.readline() # 每次读取一行
  23. list = fin.readlines() # 读取多行,返回一个列表,每行作为列表的一个元素
  24. fin.next() # 读取改行,指向下一行
  25. # 用列表来写入多行
  26. fin.writelines(list)  
  27. # 移动指针
  28. fin.seek(0, 0)  
  29. fin.seek(0, 1)  
  30. fin.seek(-1, 2)  
  31. # 提交更新
  32. fin.flush() # 平时写数据需要close才真正写入文件,这个函数可以立刻写入文件

2、OS模块

         os模块提供了很多对系统的操作。例如对目录的操作等。我们需要用import os来插入这个模块以便使用。

[python] view plaincopy

  1. #########################
  2. ## OS module
  3. ## directory operation should import this
  4. import os  
  5. os.mkdir('xiaoyi') # mkdir
  6. os.makedirs('a/b/c', mode = 666) # 创建分级的目录
  7. os.listdir() # ls 返回当前层所有文件或者文件夹名到一个列表中(不包括子目录)
  8. os.chdir() # cd
  9. os.getcwd() # pwd
  10. os.rmdir() # rm

3、目录遍历

       目录遍历的实现可以做很多普遍的功能,例如杀毒软件,垃圾清除软件,文件搜索软件等等。因为他们都涉及到了扫描某目录下所有的包括子目录下的文件。所以需要对目录进行遍历。在这里我们可以使用两种方法对目录进行遍历:

1)递归

[python] view plaincopy

  1. #!/usr/bin/python
  2. #coding:utf8
  3. import os  
  4. def dirList(path):  
  5.     fileList = os.listdir(path)  
  6.     allFile = []  
  7. for fileName in fileList:  
  8. # allFile.append(dirPath + '/' + fileName) # the same as below
  9.         filePath = os.path.join(path, fileName)  
  10. if os.path.isdir(filePath):  
  11.             dirList(filePath)  
  12.         allFile.append(filePath)  
  13. return allFile  

2)os.walk函数

[python] view plaincopy

  1. # os.walk 返回一个生成器,每次是一个三元组 [目录, 子目录, 文件]
  2. gen = os.walk('/')  
  3. for path, dir, filelist in os.walk('/'):  
  4. for filename in filelist:  
  5.         os.path.join(path, filename)  

十、异常处理

      异常意味着错误,未经处理的异常会中止程序运行。而异常抛出机制,为程序开发人员提供一种在运行时发现错误,并进行恢复处理,然后继续执行的能力。

[python] view plaincopy

  1. ###################################
  2. ### 异常处理
  3. # 异常抛出机制,为程序开发人员提供一种在运行时发现错误,
  4. # 进行恢复处理,然后继续执行的能力
  5. # 用try去尝试执行一些代码,如果错误,就抛出异常,
  6. # 异常由except来捕获,并由我们写代码来处理这种异常
  7. try:  
  8.     fin = open("abc.txt")  
  9. print hello  
  10. ### your usually process code here
  11. except IOError, msg:  
  12. print "On such file!"
  13. ### your code to handle this error
  14. except NameError, msg:  
  15. print msg  
  16. ### your code to handle this error
  17. finally: # 不管上面有没有异常,这个代码块都会被执行
  18. print 'ok'
  19. # 抛出异常,异常类型要满足python内定义的
  20. if filename == "hello":  
  21. raise TypeError("Nothing!!")  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年02月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档