专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Python » python:Python splitlines使用窍门技巧 »正文

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

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: