彭南特,毛泽全,8月23日是什么节日
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)))
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
相关文章:
-
-
python如何查看网页代码
用python查看网页代码的方法:1、使用“import”导入requests包import requests2、使用requests包的get()函数通过网页...
[阅读全文]
-
-
python如何保存文本文件
python保存文本文件的方法:使用python内置的open()类可以打开文本文件,向文件里面写入数据可以用write()函数,写完之后,使用close()函...
[阅读全文]
-
python如何编写win程序
python可以编写win程序。win程序的格式是exe,下面我们就来看一下使用python编写exe程序的方法。编写好python程序后py2exe模块即可将...
[阅读全文]
-
-
-
-
-
-
网友评论