国产探花免费观看_亚洲丰满少妇自慰呻吟_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)))
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 芮城县| 北碚区| 汉川市| 河北区| 眉山市| 昌邑市| 通河县| 渝中区| 大安市| 长武县| 萨迦县| 西城区| 龙南县| 全南县| 富裕县| 大竹县| 新化县| 桂阳县| 金门县| 吉安县| 尼玛县| 江西省| 玉树县| 康乐县| 峨眉山市| 韶关市| 衡水市| 丰都县| 南阳市| 连平县| 东城区| 万山特区| 九江市| 曲周县| 哈密市| 荆州市| 景洪市| 吉安县| 远安县| 乐平市| 绍兴市|