python:Python splitlines使用窍门技巧来源: 发布时间:星期日, 2009年9月6日 浏览:52次 评论:0
复制代码 代码如下:
mulLine = """Hello!!! Wellcome to Python's world! There are a lot of eresting things! Enjoy yourself. Thank you!""" pr ''.join(mulLine.splitlines) pr '------------' pr ''.join(mulLine.splitlines(True)) 输出结果: Hello!!! Wellcome to Python's world! There are a lot of eresting things! Enjoy yourself. Thank you! ------------ Hello!!! Wellcome to Python's world! There are a lot of eresting things! Enjoy yourself. Thank you! 利用这个就可以非常方便写些段落处理了比如处理缩进等思路方法如Cookbook书中例子: 复制代码 代码如下: def addSpaces(s, numAdd): white = " "*numAdd white + white.join(s.splitlines(True)) def numSpaces(s): [len(line)-len(line.lstrip( )) for line in s.splitlines( )] def delSpaces(s, numDel): numDel > min(numSpaces(s)): raise ValueError, "removing more spaces than there are!" '\n'.join([ line[numDel:] for line in s.splitlines( ) ]) def unIndentBlock(s): delSpaces(s, min(numSpaces(s))) 0
相关文章
读者评论发表评论 |