最近在处理数据的时候,想把一个字符串开头的“)”符号去掉,所以使用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
'\xef\xbc\x89'
>>> b
'\xef\xbc\x88'
>>> 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'\uff09\u6c42\u6559python\u9ad8\u624b\uff1a\uff08\u4e00\uff09'
>>> targetStr = targetStr.lstrip(u")")
>>> targetStr
u'\u6c42\u6559python\u9ad8\u624b\uff1a\uff08\u4e00\uff09'
>>> print targetStr
求教python高手:(一) >>> targetStr = u"(1)求教python高手:(一)"
>>> print targetStr
(1)求教python高手:(一)
>>> targetStr
u'\uff081\uff09\u6c42\u6559python\u9ad8\u624b\uff1a\uff08\u4e00\uff09'
>>> targetStr = targetStr.lstrip(u")")
>>> targetStr
u'\uff081\uff09\u6c42\u6559python\u9ad8\u624b\uff1a\uff08\u4e00\uff09'
>>> print targetStr
(1)求教python高手:(一)

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

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

>>> c = int("\t22\n")
>>> c
22

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

Python误区之strip,lstrip,rstrip的更多相关文章

  1. Python strip lstrip rstrip使用方法(字符串处理空格)

    Python strip lstrip rstrip使用方法(字符串处理空格)   strip是trim掉字符串两边的空格.lstrip, trim掉左边的空格rstrip, trim掉右边的空格 s ...

  2. python strip() lstrip() rstrip() 使用方法

    Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除最左边的字符,rstrip用于去除最右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入 ...

  3. 【转】Python中string的strip,lstrip,rstrip用法

    Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是 ...

  4. ython strip lstrip rstrip使用方法

    Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是一 ...

  5. python之字符串strip、rstrip、lstrip的方法

    1.描述 strip():用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列 rstrip():用于移除字符串右边指定的字符(默认为空格或换行符)或字符序列 lstrip():用于移除字符串 ...

  6. unicodedata.normalize()/使用strip()、rstrip()和lstrip()/encode和decode 笔记(具体可看 《Python Cookbook》3rd Edition 2.9~2.11)

    unicodedata.normalize()清理字符串 # normalize()的第一个参数指定字符串标准化的方式,分别有NFD/NFC >>> s1 = 'Spicy Jala ...

  7. python 知识 rstrip,strip,lstrip

    rstrip,strip,lstrip 作用:去除字符串中的空格或指定字符 一.默认用法:去除空格str.strip()  : 去除字符串两边的空格str.lstrip() : 去除字符串左边的空格s ...

  8. Python中的strip()的理解

    在看到Python中strip的时候产生了疑问 strip() 用于移除字符串头尾指定的字符(默认为空格) 开始测试: >>> s = 'ncy_123.python' >&g ...

  9. python3----strip lstrip rstrip

    Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符.这三个函数都可传入一个参数,指定要去除的首尾字符.注意的是,传入的是一个字符数 ...

随机推荐

  1. erlang-sunface的博客地址

    erlang-sunface的博客地址: http://blog.csdn.net/abv123456789/article/category/2206185

  2. hdu 2025:查找最大元素(水题,顺序查找)

    查找最大元素 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. 【python】NLTK好文

    From:http://m.blog.csdn.net/blog/huyoo/12188573 nltk是一个python工具包, 用来处理和自然语言处理相关的东西. 包括分词(tokenize), ...

  4. 漫游Kafka入门篇之简单介绍(1)

    介绍 Kafka是一个分布式的.可分区的.可复制的消息系统.它提供了普通消息系统的功能,但具有自己独特的设计.这个独特的设计是什么样的呢?   首先让我们看几个基本的消息系统术语: Kafka将消息以 ...

  5. python3----scrapy(笔记)

    import scrapy import sys # import io # sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb ...

  6. Ubuntu 16.04 LTS 安装libvips出现”Package vips was not found in the pkg-config search path”

    使用libvips来操作图像,libvips的部署参考一个Node.js工程:https://github.com/lovell/sharp 在MAC下安装很顺利,到Linux环境下(Ubuntu 1 ...

  7. $_SERVER,IP,域名常用方法

    PHP $_SERVER详解   $_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 $_SERVER['REMOTE_ADDR'] //当前用户 IP . $_SERVE ...

  8. 编程之美 set 8 区间重合判断

    Leetcode 上有连续的两道题, 一个是 insert interval, 一个是 merge interval, 其中 insert interval 是 merge interval. 其中 ...

  9. Linux命令之乐--expr

    计算字符长度 [root@Director ~]# echo $var hello world [root@Director test]# expr length "$var" 数 ...

  10. 自学Ajax

    使用Ajax快捷函数 说明 出于简化AJAX开发工作的流程,jQuery提供了若干了快捷函数. 实例 1.显示 test.php 返回值(HTML 或 XML,取决于返回值). $.get(" ...