Some notes about the ex41.py of Learn Python The Hard Way
各功能的运用
针对下列ex41中用到的相关模块与功能,学习并掌握。
模块:sys,random,urlopen
函数或方法:readlines,strip,join,append,capitalize,count,replace
sys
- sys.argv()
sys.argv()的第一个默认参数,即sys.argv(0),为程序本身。
带第一个参数运行时,此参数为sys.argv(1)。
比如本例中,不带参数或是带不是english的任意参数运行时:
python ex41.py,或python ex41.py a b c,PHRASES_FIRST 均为False;
带english 参数运行时,PHRASES_FIRST 均为Ture。
两种方式的执行结果不同。
PHRASES_FIRST =False
if len(sys.argv) == 2 and sys.argv[1] == "english"
PHRASES_FIRST = Ture
random
- random.sample(words, n),从字符串或列表words中随机取n个元素,返回片段。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import random
str = ['a', 'b', 'c']
print random.sample(str, 2)
输出如下:
['a', 'c']
- random.randint(m,n),在m到n(包括m,n)中产生一个随机数并返回该数。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import random
print random.randint(1,3)
输出如下:
2
- random.shuffle(words),洗牌,随机重排。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import random
list = [3, 1, 6, 7];
random.shuffle(list)
print "Random sorted the list : ", list
random.shuffle(list)
print "Random sorted the list : ", list
输出如下:
[1, 6, 3, 7]
[7, 1, 6, 3]
urlopen
urlopen()用于打开超链接文件,打开的是整个文件。
join()
join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。
比如(’-’).join(str),则str中的元素’a’,‘b’,‘c’连接在一起,变成’a-b-c’。注意,是要加入的字符在前,序列在后。这个功能应该应用较多。
#!/usr/bin/python
str = ['a', 'b', 'c']
seq = '-'
print seq.join(str)
输出以下结果:
a-b-c
append()
用于在列表中加入新的对象。在字符串操作中使用很多。
结合上面join方法以及random模块,比如:
#!/usr/bin/python
import random
str = ['a', 'b', 'c']
word = 'd'
str.append(word)
print str
str.append('-'.join(random.sample(str, 2)
print str
输出如下一种情况:
['a', 'b', 'c','d']
['a', 'b', 'c', 'd', 'b-d']
readlines()
常与文件打开操作一起出现,逐行读入所有行。与readline()的区别是readline()一次只读入一行。所以当文件大小未知或是文件大小超过内存,要慎用。这种情况下可以用循环搭配readline()的方式。
在本例中,如果将readlines改成readline,则只读入一行,那么每次取的都是是单个的字母。
***.***(o, c)
>
ANSWER: From *** get the ***function, and call it with parameters self, o, c.
class N(object)
def __init__(self, u):
>
ANSWER: Class N has-a __init__ that takes self and u parameters.
capitalize()
capitalize()是将首字母大写的方法,后面如果有大写的则变为小写。如果是首字符为空格,则只将后面大写的变为小写,空格保留。
#!/usr/bin/python
str1 = 'i am Pier.'
str2 = ' i am Pier.'
print str1.capitalize()
print str2.capitalize()
输出如下:
I am pier.
i am pier.
strip()
strip()函数可以去除字符串两端的空格,包括\n,\t,或是指定的字符等。结合上面的capitalize()可以写出如下代码:
#!/usr/bin/python
str1 = 'i am Pier.'
str2 = ' i am Pier.'
print str1.capitalize()
print str2.capitalize()
print str2.strip().capitalize()
输出如下:
I am pier.
i am pier.
I am pier.
replace()
replace用新的字符串替代指定的字符串。
str.replace(old, new, 1)
在本例中,用随机获取的字符串来取代字典中***,%%%,@@@等字符,以实现方便的替换工作。
count()
对字符串中的字符或部分字符串进行计数,并返回计数值。
结合replace()与count(),如下:
#!/usr/bin/python
str = "my family name is ***, and my brother's family name is ***,amd my sister's family name is ***"
f_name = "xu"
print str.replace("***", f_name)
print str.replace("***", f_name,1)
print str.replace("***", f_name,2)
print str.replace("***", f_name, str.count("***"))
输出:
my family name is xu, and my brother's family name is xu,amd my sister's family name is xu
my family name is xu, and my brother's family name is ***,amd my sister's family name is ***
my family name is xu, and my brother's family name is xu,amd my sister's family name is ***
my family name is xu, and my brother's family name is xu,amd my sister's family name is xu