欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python splitlines使用技巧

 更新時間:2008年09月06日 14:09:13   作者:  
Python中的splitlines用來分割行。當傳入的參數(shù)為True時,表示保留換行符 \n。通過下面的例子就很明白了
復制代碼 代碼如下:

mulLine = """Hello!!!
Wellcome to Python's world!
There are a lot of interesting things!
Enjoy yourself. Thank you!"""

print ''.join(mulLine.splitlines())
print '------------'
print ''.join(mulLine.splitlines(True))

輸出結(jié)果:
Hello!!! Wellcome to Python's world! There are a lot of interesting things! Enjoy yourself. Thank you!
------------
Hello!!!
Wellcome to Python's world!
There are a lot of interesting things!
Enjoy yourself. Thank you!

利用這個函數(shù),就可以非常方便寫一些段落處理的函數(shù)了,比如處理縮進等方法。如Cookbook書中的例子:

復制代碼 代碼如下:

def addSpaces(s, numAdd):
white = " "*numAdd
return white + white.join(s.splitlines(True))
def numSpaces(s):
return [len(line)-len(line.lstrip( )) for line in s.splitlines( )]
def delSpaces(s, numDel):
if numDel > min(numSpaces(s)):
raise ValueError, "removing more spaces than there are!"
return '\n'.join([ line[numDel:] for line in s.splitlines( ) ])
def unIndentBlock(s):
return delSpaces(s, min(numSpaces(s)))

相關(guān)文章

最新評論