最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【详解】python中字符串的strip(),lstrip(),rstrip()的含义

Python crifan 13285浏览 0评论

【问】

Hi Crifan,

我在http://bbs.csdn.net/topics/390361293 里看到抓取网易公开课的脚本,我看了下,感觉还比较简单,但是有一处不是很理解

它在 获取课程名称的时候用到以下代码,

fileName=name.contents[0].strip() .lstrip() .rstrip(‘,’) + name.a.string.strip() .lstrip() .rstrip(‘,’)

我在网上搜了下 ,

http://www.cnblogs.com/pylemon/archive/2011/05/18/2050179.html 讲到了strip,但是不理解,

后来又去 http://docs.python.org/2/library/string.html?highlight=strip#string.lstrip 看了下,还是不理解,你能帮忙看下这个脚本吗??谢谢了!!

 

【解答】

1. 参考,你自己给的,python官网的解释:
http://docs.python.org/2/library/string.html?highlight=strip#string.lstrip

主要是:

string.lstrip(s[, chars])

Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on.

Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.

string.rstrip(s[, chars])

Return a copy of the string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string this method is called on.

Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.

string.strip(s[, chars])

Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.

Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.

其中很明显的是:

lstrip = left strip =去除(字符串)左边的=stip leading=去除(字符串)开始的

rstrip = right strip =去除(字符串)右边的=strip trailling=去除(字符串)末尾的

strip = stip left and right = 去除(字符串的)左边的和右边的=strip leading and trailing = 去除(字符串)开始的和末尾的

 

针对strip的详细解释,翻译过来,就是:

对于输入的字符串,去除,开始的,和,末尾的,字符;
所要去除的字符,是你通过参数chars所指定的。
如果不指定该参数,或者为None,

(即形式为:

someStringValue.strip()

someStringValue.strip(None)

someStringValue.strip(chars=None)

则默认为白空格Whitespace。
所谓的白空格,一般指的是:
空格本身,回车\r,换行\n,制表符\t, 换页符\f

(对应于正则表达式中的: \s == [ \r\n\t\f] )

所以,很明显的是:

someString.strip() == someString.lstrip().rstrip()

 

而关于此处的:

name.contents[0].strip() .lstrip() .rstrip(‘,’)

我觉得是,其多写了个lstrip(),应该改成:

name.contents[0].strip().rstrip(‘,’)

其等价于

name.contents[0].strip(None).rstrip(‘,’)

意思是
对于name.contents[0],
先去除首尾(即头部和尾部)的白空格(空格本身,回车\r,换行\n,制表符\t, 换页符\f )
之后,再去除尾部的逗号’,’

假如,原先字符串是:

"   some string value,,,      "

经过这么一处理,则很明显,去除首尾的白空格后,再去除尾部的逗号,就变成了:

"some string value"

了。

 

进一步举例说明:

strip一般用来是去除一个字符的首尾的多余的,不可见的字符(所谓的白空格)。

比如一个字符是:

demoStr = "      hello wold !    "

那么:

demoStr.lstrip() = 去除left左边的白空格  = "hello wold !    "
demoStr.rstrip() = 去除right右边的白空格 = "      hello wold !"

demoStr.strip() = demoStr.lstrip().rstrip()=去除left左边和right右边=去除首尾的白空格="hello wold !"


这下明白了吧?

转载请注明:在路上 » 【详解】python中字符串的strip(),lstrip(),rstrip()的含义

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (4)

  1. 谢谢前辈的文章,窝也正好在这里有疑惑,学习到了。
    N9年前 (2015-07-26)回复
  2. Crifan老师, 根据你的解释,我去看了下网易公开课源码 # http://v.163.com/special/opencourse/paradigms.html 1. name.contents[0].strip() .lstrip() .rstrip(',') 抓取范围为 [第1集] 编程范式1 <!-- 中的 [第一集], 但是去除空格和"," 怎么能提取到"[第一集]"呢 2.name.a.string.strip() .lstrip() .rstrip(',') 抓取范围为 编程范式1 中的 编程范式1 , 但是去除空格和"," 怎么能提取到"编程范式1"呢 不好意思,我比较麻烦,问题比较多哈.
    Thanks11年前 (2013-04-30)回复
    • 悲剧了,我发出来的好多源码都被 直接转换为链接了....
      Thanks11年前 (2013-04-30)回复
    • 已回复。
      crifan11年前 (2013-05-06)回复
89 queries in 0.163 seconds, using 22.17MB memory