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函数切割错误

  • 相关阅读:
    看《你必须知道的.NET》有感工厂模式的另类解读
    学习笔记:PDO
    学习笔记:mysqli预处理和事务处理
    学习笔记:mysql
    学习笔记:基础+文件操作(上传|下载)+面向对象+xml
    学习笔记:php+mysql
    学习及求职心得|PHP学习心得|IT从业者的几点体会
    flash做登录页传递值给asp.net,其他语言的也可以参照下
    rpm安装依赖问题
    让进程在后台可靠运行
  • 原文地址:https://www.cnblogs.com/lxw0109/p/python_strip_lstrip_rstrip.html
Copyright © 2011-2022 走看看