最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python splitlines使用技巧

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

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!

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

復(fù)制代碼 代碼如下:

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)文章

最新評(píng)論

鄂温| 日土县| 青铜峡市| 驻马店市| 大名县| 开鲁县| 余庆县| 金溪县| 乐清市| 安达市| 双牌县| 广宗县| 治县。| 阳曲县| 大城县| 大城县| 乌拉特后旗| 林口县| 布尔津县| 乾安县| 江油市| 大余县| 宽城| 克什克腾旗| 富锦市| 抚顺市| 寻乌县| 许昌市| 西充县| 罗江县| 武安市| 广灵县| 新邵县| 长寿区| 天等县| 资源县| 信宜市| 桃江县| 印江| 九寨沟县| 青阳县|