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

首頁 > 編程 > Python > 正文

Python splitlines使用技巧

2020-02-23 04:40:12
字體:
來源:轉載
供稿:網友
代碼如下:
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))

輸出結果:
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!

利用這個函數,就可以非常方便寫一些段落處理的函數了,比如處理縮進等方法。如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)))
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 横峰县| 石门县| 商城县| 怀来县| 台南县| 团风县| 迁西县| 东台市| 平湖市| 东宁县| 三江| 定兴县| 罗山县| 青岛市| 陈巴尔虎旗| 班玛县| 盐亭县| 茌平县| 瓮安县| 电白县| 偏关县| 和政县| 凤翔县| 泰宁县| 施秉县| 滨海县| 江阴市| 黔江区| 德钦县| 大港区| 抚顺县| 东港市| 扎赉特旗| 清新县| 连平县| 乡宁县| 吴旗县| 安宁市| 武强县| 于田县| 灵川县|