「学习笔记——Python」Googles Python Class 学习笔记Googles Python Class学习完了《The Python Tutorial》后
「学习笔记——Python」Google's Python Class 学习笔记
Google's Python Class
学习完了《The Python Tutorial》后,又学习了一下Google's python class,感觉比前者要好。关键是提供了一些习题,让你通过这些习题了解Python的特性,让你尽快用Python去解决一些问题,做完这些习题,感觉比看完整本书还有感觉。这给我提了个醒,学完一个东西要主动找一些练习去做,不然看多少书都没有感觉。尤其是一些实践性很强的东西。下面是读的过程中记的一些简要的笔记。
1 Python Introduction
函数需要在使用前定义,所以main函数一般放在最后使用空格,而不是TAB来缩进,可以设置编辑器使用空格代替TAB.空格和TAB混用会带来语法错误, 尽管看起来缩进的距离是一样的。代码只有在运行时才会检查,如果你调用函数时写错了函数名,且从来没有执行到调用这个函数的地方, 那么python会运行良好,它不会像其它语言一样在编译时对代码进行检查。最好使用 import sys,然后调用sys模块时使用sys.argv,而不要使用 from sys import argv,然后调用 时使用 argv.前者会让我们一眼就看出argv来自哪里。可以使用help函数得到函数和模块的文档,例如help(len)2 Python String
String 方法:方法和函数类似,只不过方法是“运行在”对象上.s.lower(),s.upper() – 大小写转换s.trip() – 去掉字符串前后的空白s.isalpha()/s.isdigit()/s.isspace() – 测试字符串是不是全部都是指定类型s.startwith('other'), s.endwith('other') – 测试是否以指定字符串开始/结尾s.find('other') – 查找子串,如果有,返回第一个下标,如果没有,返回-1s.replace('old','new') – 将字符串中所有 'old' 替换为 'new's.split('delim') – 返回以'delim'分割的子串lists.join(list) – 与split相反,将list中的元素以字符串s连接起来3 Python List
List赋值操作并不会创建一份新数据,例如 c是个List,b = c执行后,b 和 c 指向同一内存区域。in: 可以用于判断一个元素是否在一个集合中。例如:for num in squares:if 'curly' in mlist:if character in mstring:List常用函数:list.append(elem) – 在list的最后添加一个元素,注意,这会改变list,而不是返回新的listlist.insert(index,elem) – 在list的index处插入一个元素list.extend(list2) – 将list2添加到list的最后,也可以使用+ 或+=得到同样结果list.index(elem) – 搜索元素,返回下标list.remove(elem) – 搜索元素,然后删除(如果不存在会返回ValueError)list.sort() – 对list进行排序list.pop(index) – 删除并返回指定位置的元素4 Python Sorting
>>> str = 'purple alice-b@google.com monkey dishwasher'>>> import re>>> match = re.search('([\w.-]+)@([\w.-]+)', str)>>> print match.group()alice-b@google.com>>> print match.group(2)google.com>>> print match.group(1)alice-b>>> matchx = re.search('([\w.-]+)(@)([\w.-]+)', str)>>> print matchx.group()alice-b@google.com>>> print matchx.group(1)alice-b>>> print matchx.group(2)@>>> print matchx.group(3)google.com
findallre.search只找第一个匹配的,findall找所有匹配的
>>> str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher'>>> emails = re.findall(r'[\w\.-]+@[\w\.-]+', str)>>> emails['alice@google.com', 'bob@abc.com']
findall还可以用于在文件中查找strings = re.findall(r'some pattern', f.read())findall与group
>>> str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher'>>> tuples = re.findall(r'([\w\.-]+)@([\w\.-]+)', str)>>> tuples[('alice', 'google.com'), ('bob', 'abc.com')]
选项:search()和findall()都有相应参数作为选项IGNORECASE :匹配时忽略大小写DOTALL :允许点号(.)匹配新行,默认情况.是不匹配新行的MULTILINE: 如果一个字符串由多行组成,使^ $匹配每一行,而非整个字符串贪心匹配'<.*>' 会匹配到最后一个,而非第一个'>',这叫做贪心匹配,如果想使用非贪心匹配,可以用?
>>> str = '<b>foo</b> and <i>so on</i>'>>> match1 = re.search('(<.*>)', str)>>> match1.group()'<b>foo</b> and <i>so on</i>'>>> match2 = re.search('(<.*?>)', str)>>> match2.group()'<b>'
替换:re.sub(pat, replacement, str)
7 Python Utilities
File System–os,os.path,shutilos.listdir(dir) :列出目录下的所有文件名,返回一个listos.path.join(dir, filename) :把目录和文件名组合在一起,返回整个路径os.path.abspath(path):返回全路径os.path.dirname(path),os.path.basename(path):得到path中的目录,文件名os.path.exists(path):指定路径是否存在os.mkdir(dir_ path):建立一个目录os.makedirs(dir_ path):建立整个路径上所有需要的目录shutil.cpy(source_ path, dest_ path):复制文件Running External Processes – command(status, output) = commands.getstatusoutput(cmd) :运行命令,返回执行结果和输出结果output = commands.getoutput(cmd) :同上,只是不返回执行结果os.system(cmd) :同上,不返回执行结果和输出结果,但将结果输出到标准输出HTTP – urllib 和 urlparseufile = urllib.urlopen(url) :返回与url相关的类文件对象 text = ufile.read() :读取内容info = ufile.info() :请求的 meta info, 例如 Content-Language,Data, Content-Type等信息baseurl = ufile.geturl –得到请求的base url,由于有转向,可能和原来url不同urllib.urlretrieve(url, filename) –从指定的url下载文件,存储在本地的filename路径中(包含新文件名)urlparse.urljoin(baseurl,url) –得到全路径另外,这里还有一份Google Python Style Guide,描述了Google建议的Python编程风格