zoukankan      html  css  js  c++  java
  • Python误区之strip,lstrip,rstrip

          最近在处理数据的时候,想把一个字符串开头的“)”符号去掉,所以使用targetStr.lstrip(")"),发现在

    将处理完的数据插入到数据库时会出现编码报错,于是在网上搜到了这个帖子。出现上述编码错误问题的原因

    是我对lstrip函数的理解错误,权威的解释如下:

    str.lstrip([chars])

    Return a copy of the string with leading characters removed. The chars argument is a string

    specifying the set of characters to be removed. If omitted or None, the chars argument

    defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations

    of its values are stripped:

    >>> ' spacious '.lstrip()
    'spacious '
    >>> 'www.example.com'.lstrip('cmowz.')
    'example.com'

    strip, lstrip, rstrip的情况都一样,最重要的是要注意

    参数chars是一个集合,而不是prefix(chars集合中字符的所有组合都会被删除)

    所以分析我出错的情况:

    >>> a = ""
    >>> b = ""
    >>> a
    'xefxbcx89'
    >>> b
    'xefxbcx88'
    >>> len(a)
    3
    >>> a.lstrip(b)
    'x89'
    >>> b.lstrip(a)
    'x88'
    
    >>> targetStr = ")求教python高手:(一)"
    >>> print targetStr.lstrip(a)
    求教python高手:(一)
    >>> print targetStr.lstrip(b)
    �求教python高手:(一)
    
    >>> targetStr = "(1)求教python高手:(一)"
    >>> print targetStr.lstrip(a)
    �1)求教python高手:(一)
    >>> print targetStr.lstrip(b)
    1)求教python高手:(一)

    所以我采用的解决方法如下,

    方法1:

    >>> targetStr = ")求教python高手:(一)"
    >>> targetStr = targetStr[3:] if targetStr.find("") == 0 else targetStr
    >>> print targetStr
    求教python高手:(一)
    
    >>> targetStr = "(1)求教python高手:(一)"
    >>> targetStr = targetStr[3:] if targetStr.find("") == 0 else targetStr
    >>> print targetStr
    (1)求教python高手:(一)

    方法2:

    >>> len(u"")
    1
    >>> targetStr = u")求教python高手:(一)"
    >>> print targetStr
    )求教python高手:(一)
    >>> targetStr
    u'uff09u6c42u6559pythonu9ad8u624buff1auff08u4e00uff09'
    >>> targetStr = targetStr.lstrip(u"")
    >>> targetStr
    u'u6c42u6559pythonu9ad8u624buff1auff08u4e00uff09'
    >>> print targetStr
    求教python高手:(一)
    
    >>> targetStr = u"(1)求教python高手:(一)"
    >>> print targetStr
    (1)求教python高手:(一)
    >>> targetStr
    u'uff081uff09u6c42u6559pythonu9ad8u624buff1auff08u4e00uff09'
    >>> targetStr = targetStr.lstrip(u"")
    >>> targetStr
    u'uff081uff09u6c42u6559pythonu9ad8u624buff1auff08u4e00uff09'
    >>> print targetStr
    (1)求教python高手:(一)

    如果各位有更好的方法,一定要告诉我 : )

    此外,从这个帖子我还学习到了另外一点:

    >>> c = int("	22
    ")
    >>> c
    22

    References:
    求教python高手:一个简单的问题,lstrip函数切割错误

  • 相关阅读:
    [BZOJ3195] [Jxoi2012]奇怪的道路
    Splay Tree
    区间DP复习
    Link Cut Tree
    [BZOJ2734] [HNOI2012]集合选数
    如何写出杀手级简历(针对程序员) (转)
    30个提高Web程序执行效率的好经验(转)
    Oracle中的SQL跟踪( 转)
    如何终止SQL Server中的用户进程(转)
    Will the real programmers please stand up?(转)
  • 原文地址:https://www.cnblogs.com/lxw0109/p/python_strip_lstrip_rstrip.html
Copyright © 2011-2022 走看看