最新国产好看的视频,伊人天堂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)論

乐业县| 曲沃县| 靖西县| 西平县| 象州县| 宜兰市| 冀州市| 诸城市| 桦川县| 江山市| 台南县| 南宁市| 麻栗坡县| 曲沃县| 临清市| 南雄市| 博湖县| 普安县| 砚山县| 临颍县| 泌阳县| 灵璧县| 昆山市| 蒙自县| 阳东县| 浦东新区| 吉林市| 邢台市| 怀集县| 遵义县| 衡阳市| 神农架林区| 平顶山市| 布拖县| 镇平县| 台南县| 繁昌县| 古蔺县| 隆昌县| 洛扎县| 怀柔区|