国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 朝阳市| 巢湖市| 涿鹿县| 汽车| 宁城县| 文山县| 峡江县| 视频| 龙陵县| 宁河县| 穆棱市| 双城市| 濉溪县| 安陆市| 新乡市| 砀山县| 府谷县| 永善县| 图们市| 南汇区| 文登市| 山阴县| 江津市| 南投市| 永康市| 海口市| 泽库县| 嘉定区| 西充县| 陆河县| 清水县| 友谊县| 克山县| 黄大仙区| 高淳县| 盘锦市| 蓝田县| 宝兴县| 凌海市| 灵宝市| 巨鹿县|