国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python splitlines使用技巧

2019-11-25 18:46:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
復(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)))
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 镇远县| 平武县| 临颍县| 遂溪县| 锡林浩特市| 平江县| 榆社县| 长葛市| 岐山县| 凤山县| 凤凰县| 醴陵市| 亚东县| 兴业县| 英山县| 平舆县| 修水县| 常州市| 苏州市| 九龙坡区| 遂平县| 句容市| 西吉县| 衡阳市| 海口市| 米泉市| 时尚| 金堂县| 和平区| 宝应县| 康定县| 乐清市| 德安县| 舟山市| 蒲城县| 乐平市| 洮南市| 冷水江市| 搜索| 策勒县| 桃源县|