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

  • 相关阅读:
    揭开webRTC媒体服务器的神秘面纱——WebRTC媒体服务器&开源项目介绍
    打造一个上传图片到图床利器的插件(Mac版 开源)
    游戏编程十年总结(下)
    游戏编程十年总结(上)
    使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码
    手机网游实时同步方案
    Unity AssetBundle爬坑手记
    Unity3D新手引导开发手记
    敏捷开发随笔(一)高效软件开发之道
    U3D DrawCall优化手记
  • 原文地址:https://www.cnblogs.com/lxw0109/p/python_strip_lstrip_rstrip.html
Copyright © 2011-2022 走看看