最近在处理数据的时候,想把一个字符串开头的“)”符号去掉,所以使用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. linux文件目录连接

    linux系统下提供ln指令来进行文件链接.文件链接主要分为硬链接和软链接. 硬链接:由于linux下的文件是通过索引节点(Inode)来识别文件,硬链接可以认为是一个指针,指向文件索引节点的指针,系 ...

  2. strust2自定义interceptor的基本方法及操作

    需求:制作一个网站需要用户登陆后才能查看,即一个权限的问题 1.首先明确在用户没登陆前有两个Action请求是可以通过的,即注册和登陆. 2.创建拦截器,如UserLoginInterceptor.j ...

  3. VBS 移除excel数据公式,只保留值

    如果将excel数据公式移除,只保留计算之后的值,将大大减少excel文件. 因为有上篇移除excel外部数据链接的经验,进行excel数据公式移除将快的多,方法如下. 首先我们得明白怎么手动移除ex ...

  4. ChemDraw中化学信息怎么通过Excel搜索

    用户可以通过ChemDraw for Excel插件功能在Office Excel中建立ChemOffice菜单将ChemOffice和Excel结合使用,使用电子表格的最大优势之一就是可以清晰查看并 ...

  5. 关于ie中实现弹性盒模型-我的css

    css3中的弹性盒模型大家都不陌生,但是能否在ie6中实现呢?第三方库中涉及到的页少之又少,也有一部分css框架中支持各种布局,下面给出我用的盒模型样式(为了以后copy方便而已): /******* ...

  6. 怎样使用DWZ?

    首先说明,这篇文章不是解说DWZ内部实现原理的,也不打算分析它的源代码,这里仅仅是演示一下,怎样将DWZ框架整合到项目中去. 刚刚过去的项目中,前台UI使用的是DWZ.因为之前项目的开发环境都已经搭建 ...

  7. Redis异构集群数据在线迁移工具Redis-Migrate-Tool【转】

    摘要:Redis-Migrate-Tool(后面都简称RMT),是唯品会开源的redis数据迁移工具,主要用于异构redis集群间的数据在线迁移,即数据迁移过程中源集群仍可以正常接受业务读写请求,无业 ...

  8. 部署全局ajax处理

    $.ajaxSetup({ beforeSend:function(){ $('.loading').show(); }, complete:function(){ $('.loading').fad ...

  9. Tomcat服务器的安装与配置

    安装 输入网址进入Tomcat的官网            在左边导航栏选择对应下载的版本            下载安装包形式             下载并解压到我们欲放入的目录中 配置      ...

  10. recyclerView中的方法

    onCreateViewHolder(); onBindViewHolder(); getItemCount(); recyclerVIew中没有添加头布局和尾布局方法.可以用getItemViewT ...