2013年10月24日

Python Cookbook学习记录 ch1_13_2013/10/24

摘要: 1.13访问子字符串a.使用切片,但是切片一次只能取得一个字段b.使用struct.unpack方法import struct# Get a 5-byte string, skip 3, get two 8-byte strings, then all the rest:baseformat = "5s 3x 8s 8s"# by how many bytes does theline exceed the length implied by this# base-format (24 bytes in this case, but struct.calcsize is g 阅读全文

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

Python Cookbook学习记录 ch1_12_2013/10/24

摘要: ch1_11 PASS1.12 控制大小写首先是大家都很熟悉的upper()和lower()文中还介绍了capitalize()和title(),capitalize()的作用是将第一个字符改成大写,第二个字符是将每个单词的第一个字符转成大写>>> print 'hello world!'.capitalize()Hello world!>>> print 'hello world!'.title()Hello World!讨论:写一个iscapitalized(s)函数,来判断字符串是不是首字符大写>>> 阅读全文

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

2013年10月23日

Python Cookbook学习记录 ch1_10_2013/10/23

摘要: 1.10 过滤字符串中不属于指定集合的字符这个函数的实现方法和上一节一样:"除去要保留的剩下的都是要删除的">>> import string>>> allchars = string.maketrans('','')>>> def makefilter(keep): delchars = allchars.translate(allchars,keep) def thefilters(s): return s.translate(allchars,delchars) return the 阅读全文

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

Python Cookbook学习记录 ch1_9_2013/10/23

摘要: 1.9 简化字符串的translate方法的使用本节是将上一节介绍的translate方法封装成一个功能强大的函数,并介绍“闭包"的概念此函数的指导思想是“'frm'和'to'两个变量获得转换方案,除去要保留的剩下的都是要删除的”将转换方案和要删除的作为两个参数传给函数translate,再将此函数赋给变量。>>> import string>>> def translator(frm='',to='',delete='',keep=None): if len(to) 阅读全文

posted @ 2013-10-23 21:46 七海之风 阅读(105) 评论(0) 推荐(0)

Python Cookbook学习记录 ch1_8_2013/10/23

摘要: 1.8 检查字符串中是否包含某字符集合中的字符a.最常用的方法,使用for循环,第一个序列式字符串,第二个集合Type "copyright", "credits" or "license()" for more information.>>> def containAny(seq,aset): for item in seq: if item in aset: return True return False>>> seq = 'abc'>>> import se 阅读全文

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

2013年10月22日

Python Cookbook学习记录 ch1_7_2013/10/22

摘要: 1.7将字符串逐字符或者逐词反转a.逐字符反转可以直接用分片来操作,这是比较常用的方法>>> Text = "Hello World">>> print Text[::-1]dlroW olleHb.逐词反转则需要做如下几步:1.使用split方法将字符串以单词为单位分割开2.将单词逆序排列3.将单词拼接起来,单词之间加入一个空格>>> Text = "Hello World, I am coming!">>> worldlist = Text.split()>>> 阅读全文

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

Python Cookbook学习记录 ch1_6_2013/10/22

摘要: 1.6合并字符串a.合并字符串最常用的应该就是“+”方法吧,用过python都知道,方便实用>>> first = 'Hello'>>> second = ' World'>>> print first +secondHello Worldb.如果是一个字符串数组的话,那么可以使用join方法将字符串拼接起来>>> wordlist = ['Hello ','World','!']>>> print ''.jo 阅读全文

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

Python Cookbook学习记录 ch1_5_2013/10/22

摘要: 1.5去除字符串两端的空格这一节的内容相当于上一节字符串对齐的逆操作。可以使用string对象的lstrip,rstrip和strip方法来完成相应的操作,分别用于删除开头,末尾和两端的空格。>>> x = ' hello '>>> print x.lstrip()+'|'hello |>>> print x.rstrip()+'|' hello|>>> print x.strip()+'|'hello|其中这几个方法都是可以带参数的,带上参数后上述方法将从左 阅读全文

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

Python Cookbook学习记录 ch1_4_2013/10/22

摘要: 1.4字符串对齐使用string对象的ljust,rjust和center来实现其中方法的第二个参数可以设置填充字符,默认为空格>>> print '|'+'hello'.ljust(20)+'|'|hello |>>> print '|'+'hello'.rjust(20)+'|'| hello|>>> print '|'+'hello'.center(20)+'|'| hello |>&g 阅读全文

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

2013年10月21日

Python Cookbook学习记录 ch1_3_2013/10/21

摘要: 1.3测试一个对象是否是字符串如果让我写代码,第一反应肯定会采用书中提到的的槽糕的类型测试,使用type(),但是这可能导致Unicode码不能通过测试,甚至任何str子类都不行。>>> def isExactlyAString(anobj): return type(anobj) is type('')>>> print isExactlyAString('abc')True>>> print isExactlyAString(u'abc')False书中推荐方法如下>>> 阅读全文

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

导航