2013年10月28日

Python Cookbook学习记录 ch2_9/16_2013/10/28

摘要: 2.9 从zip文件中读取数据可以通过Python标准库提供的zipfile模块访问zip压缩文件>>> import zipfile>>> z = zipfile.ZipFile('test.zip','r')>>> for filename in z.namelist(): print 'File:', filenameFile: wex.txtFile: 1.txtFile: a.txt2.16 遍历目录树使用os.walk可以便利目录树>>> for root, di 阅读全文

posted @ 2013-10-28 22:10 七海之风 阅读(171) 评论(0) 推荐(0)

2013年10月27日

Python Cookbook学习记录 ch2_6/7/8_2013/10/27

摘要: 2.6 处理文件中的每一词a.先获取每一行,再通过split()方法获取每一个词,之后再进行处理for line in open(thefilepath): for word in line.split(): dosomethingwith(word)b.使用生成器(generator),将元素的迭代,和元素的处理分开def words_of_file(thefilepath, line_to_words=str.split): the_file = open(thefilepath): for line in the_file: for word... 阅读全文

posted @ 2013-10-27 21:22 七海之风 阅读(129) 评论(0) 推荐(0)

Python Cookbook学习记录 ch2_4/5_2013/10/27

摘要: 2.4从文件中读取指定的行从根据给定的行号,从文本中读取一行数据使用python 标准库中linecache模块>>> import linecache>>> theline = linecache.getline('thefile.txt',3)>>> print thelineUsing this simple program as a basis, computer science principles or elements of a specific programming language can be expl 阅读全文

posted @ 2013-10-27 20:10 七海之风 阅读(142) 评论(0) 推荐(0)

Python Cookbook学习记录 ch2_3_2013/10/27

摘要: 2.3搜索和替换文件中的文本需要将文件中的某个字符串改变成另外一个。字符串中replace方法提供了字符串替换最简单的办法:>>> s = 'hello world'>>> s.replace('l','L')'heLLo worLd'搜索替换需要两个文件,先第一个文件,再通过replace方法将里面的内容进行铁环,再讲替换完的内容输入到另外一个文件中>>> file_object = open('thefile.txt')>>> file_t 阅读全文

posted @ 2013-10-27 17:42 七海之风 阅读(131) 评论(0) 推荐(0)

Python Cookbook学习记录 ch2_2_2013/10/27

摘要: 2.2写入文件a.将一个长字符串写入文件:open('thefile.txt', 'w').write(all_the_text) # text to a text fileopen('abinfile', 'wb').write(all_the_data) # data to a binary fileb.先付给一个对象,处理完毕后在关闭文件file_object = open('thefile.txt', 'w')file_object.write(all_the_text)file_objec 阅读全文

posted @ 2013-10-27 17:24 七海之风 阅读(119) 评论(0) 推荐(0)

Python Cookbook学习记录 ch2_1_2013/10/27

摘要: 开始第二章的学习,第二章讲的是python对文件的操作2.1读取文件既然是文件操作,那么就必须先有文件,在如下目录“E:\PythonCookbook\CHA2”创建文件后,在交互模式下发现python无法直接读取文件,文件木有找到>>> all_the_text = open('thefile.txt').read()Traceback (most recent call last): File "", line 1, in all_the_text = open('thefile.txt').read()IOError: 阅读全文

posted @ 2013-10-27 16:52 七海之风 阅读(170) 评论(0) 推荐(0)

Python Cookbook学习记录 ch1_19_2013/10/27

摘要: 1.19检查字符串中的结束标记系统有内建函数startswith()和endswith()来检查字符串的开始和结束字符1 >>> s='hello'2 >>> s.endswith('o')3 True4 >>> s.startswith('h')5 True本节使用方法检查字符串S中是否包含多个结束标记中的一个。文中推荐了一种快捷优雅的方法:>>> import itertools>>> def anyTrue(predicate,sequence): r 阅读全文

posted @ 2013-10-27 15:35 七海之风 阅读(152) 评论(0) 推荐(0)

2013年10月25日

Python Cookbook学习记录 ch1_17_2013/10/25

摘要: 1.16替换字符串中的字串给定一个字符串,通过查询一个替换字典,将字典中被标记的子字符串替换掉>>> new_style = string.Template('this is $fst and this is $snd')>>> print new_style.substitute(fst=5,snd=10)this is 5 and this is 10>>> print new_style.substitute({'fst':5,'snd':10})this is 5 and this i 阅读全文

posted @ 2013-10-25 21:34 七海之风 阅读(170) 评论(0) 推荐(0)

Python Cookbook学习记录 ch1_15_2013/10/25

摘要: 1.15扩展和压缩制表符tab和空格的互转,一般是tab转空格,一个expandtabs()就足够了,空格转tab可能只出在考试题中s = "a\t aaaaa\t aaaa" s1 = s.expandtabs() print s,len(s) print s1,len(s1) #把空格转成tab def unexpand(s,tablen = 8): import re #切分成空格和非空格 pieces = re.split(r'( +)',s.expandtabs()) #记录当前字符串总长度 len... 阅读全文

posted @ 2013-10-25 21:08 七海之风 阅读(125) 评论(0) 推荐(0)

Python Cookbook学习记录 ch1_14_2013/10/25

摘要: 1.14 改变多行文本字符串的缩进>>> def reindent(s,spacenum): leadspace = ' '*spacenum lines = [leadspace+line.strip() for line in s.splitlines()] return '\n'.join(lines)确保文本每一行直接的相对缩进,这样子需要提前计算每一行行首的空格数(下述代码没有按照书中的方式进行处理)>>> def numspaces(s): return [len(line)-len(line.lstrip()) 阅读全文

posted @ 2013-10-25 20:59 七海之风 阅读(158) 评论(0) 推荐(0)

导航