python基础教程(第二版)
来源:互联网 发布:国产羽绒服 知乎 编辑:程序博客网 时间:2024/05/22 15:50
第十章 自带电池
导入模块:
import syssys.path.append('c:/python27')//告诉程序到这里找模块
1.1. 若模块是一个程序,则在导入时只执行一次,后面导入不在执行,修改后可以用reload
函数重新导入,这时会重新执行。
#hello2.pydef hello(): print 'Hello,World!'>>>import hello2>>>hello2.hello()hello,world!#hello3.pydef hello(): print "Hello,world!"#A test 测试模块hello()>>>import hello3**Hello,World!**#这不是想要的结果>>>hello3.hello()Hello,World!#解决方法:使用__name__变量#hello4.pydef hello(): print 'Hello,World!'def test(): hello()if __name__ == '__main__':test()>>>import hello4#达到预期效果>>>hello4.hello()
2.把模块放进目录的几种方法:
2.1 把模块放进sys
中的path
路径中找。
>>>import sys
2.2 编辑sys.path
,还得在环境变量中实现PYTHONPATH
,它包含模块所在的目录。
2.3 命名模块。
包含代码的文件的名字和模块相同。
包
- 包含一个命名为
__init__.py
的文件。
- 包含一个命名为
模块包含的内容:
dir函数: 列出模块包含内容:dir.copy()
all变量:从模块导入所有名字包括什么含义:copy.__all__
:['Error','copy','deepcopy']
help:获取帮助
doc:查看文档 :range.__doc__
file: 使用源代码:copy.__file__
标准库
3.1sys
模块
3.2 os
模块:提供访问多个操作系统服务的功能。
3.3 fileinput
模块:读写文件。
4. 集合,堆和双端队列
4.1
set(range(10))
set([0,1,2,3,4,5,6,7,8,9])
4.2 集合
集合是可变的不能用作字典中的键。
4.3 堆
4.4 双端队列
- time模块
功能:获得当前时间,操作时间和日期,从字符串读取时间及格式化时间为字符串。
4.5 random模块
返回随机数的函数
4.6 shelve模块
存储数据。
4.6.1 使用shelve模块修改临时变量时,要将临时变量绑定到副本上,并且在它被修改后重新存储这个副本。
import shelves = shelv.open('test.dat')s['x'] = ['a','b','c']s['x'].append('d')>>>s['x']['a','b','c']#解决方法,临时变量temp = s['x']temp.append('d')s['x'] = temp>>>s['x']['a','b','c','d']
4.7 re
模块
4.7.1
通配符(.):匹配任何字符
转义():对特殊字符转义
字符集([]):[^abc]反转字符,除了abc以外的任意字符。
选择字符或子模式(|):’p(ython|er)’
4.7.2 re模块的一些重要函数
4.7.3匹配对象和组
group(0):表示整个字符串。
group(m):表示左边有m个左括号的m组
4.7.4贪婪和非贪婪模式
加上(?):是非贪婪的,即最少匹配原则
(.+)(.+?)
#贪婪模式>>>emphasis_pattern = r'\*(.+)\*'>>>re.sub(emphasis_pattern,r'<em>\1</em>','*This* is *it*!')'<em>This* is *it</em>!'#非贪婪模式>>>emphasis_pattern = r'\*(.+?)\*'>>>re.sub(emphasis_pattern,r'<em>\1</em>','*This* is *it*!')'<em>This</em> is <em>it</em>!'
4.7.5 模板系统
模板是通过放入具体值从而得到某种已完成的文件。
#template.pyimport fileinput,refield_pat = re.compile(r'\[(.+?)\]')scope = {}def replacement(match): code = match.group(1) try: return str(eval(code,scope)) except SyntaxError: exec code in scope return ''lines = []for line in fileinput.input(): lines.append(line) text = ''.join(lines)print field_pat.sub(replacement, text)#data.txt[x=2][y=3]The sum of [x] and [y] is [x+y]#运行python template.py data.txt#结果The sum of x and y is 5
- python基础教程(第二版)
- Python基础教程(第二版) 评价
- 笔记-Python基础教程(第二版)第一章
- Python 基础教程(第二版)读书笔记
- 笔记-Python基础教程(第二版)第二章
- Python基础教程第二版读书笔记-第二章
- *python基础教程(第二版)(学习笔记)*
- Python 基础教程(第二版)(第十~十一章)
- 《Python基础教程第二版》学习笔记(一)第一章 基础知识
- 学习《Python基础教程(第二版)》笔记1
- 学习《Python基础教程(第二版)》笔记2--基础知识
- 学习《Python基础教程(第二版)》笔记4---使用字符串
- 学习《Python基础教程(第二版)》笔记5---字典
- 学习《Python基础教程(第二版)》笔记9--抽象1
- 学习《Python基础教程(第二版)》笔记10--抽象2
- Python基础教程(第二版)下载地址
- Python - 基础教程学习(第一章 & 第二章)
- 读《python基础教程》(第二章)
- 移动平台对 META 标签的定义
- 【位运算 && 线段树】SDUT 3930 皮卡丘的梦想2
- 解决乱码
- 转吴军数学之美对几种类型的科学从事人员
- SIT与UAT的分别
- python基础教程(第二版)
- Docker挂载主机目录Docker访问出现Permission denied的解决办法
- 从spark streaming checkpoint文件中还原数据
- 服务端指南 | HTTPS 项目实战指南
- ionic浏览器运行
- 在应用中直接打开QQ聊天
- 【重大更新】DevExpress v17.1新版亮点(XAF篇)
- Html中文字过多,单行超出和多行超出显示省略号
- h2错误提示:Table not found