最近在处理数据的时候,想把一个字符串开头的“)”符号去掉,所以使用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. 学习shader之前必须知道的东西之计算机图形学(一)渲染管线

    引言 shader到底是干什么用的?shader的工作原理是什么? 其实当我们对这个问题还很懵懂的时候,就已经开始急不可耐的要四处搜寻有关shader的资料,恨不得立刻上手写一个出来.但看了一些资料甚 ...

  2. java基础知识总结8(数据库篇1)

    一. Oracle的安装(windowXP.win7.Linux)和卸载 1.1 Oracle的安装 1.1.1 在WindowsXP.Win7下安装 第一:解压win32_11gR2_databas ...

  3. c#用run32dll打开系统dll(如系统图片查看器,并置最顶层)

    [DllImport("user32.dll", EntryPoint = "SetWindowPos",CharSet = CharSet.Auto)] st ...

  4. angularJs多文件上传

    <input type="file" id="file{{$index}}" class="file{{$index}}" ngf-s ...

  5. 第一次广搜!HDU1548--A Strange Lift

    一上来看见题目就用了深搜(因为只会深搜)果断内存超限(据说时间也会超限)无奈只好开始用广搜 其实广搜的思路和深搜有很多类似的地方 不过实现的过程中用到了队列 因此有点难以理解(好吧我个人认为) 这题是 ...

  6. c#基础 第四讲

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. css如何引入外部字体?

    第一步,在CSS中引入字体并给名字取一个合适的名字,如下 1 2 3 4 5 6 7 @font-face {     /* font-properties */     font-family: p ...

  8. c#使用FastReports打印

    private void btnprint_Click(object sender, EventArgs e) { //报表路径 string path = Application.StartupPa ...

  9. nodejs(一)

    nodejs第一章节 使用node来实现第一个http服务器 var http = require(‘http’); http.createServer(function (request, resp ...

  10. puppeteer部署到centOS上出现launch chrome fail的情况

    在Mac上调试无问题,放到阿里云上运行会报错. 需要先安装依赖, yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 lib ...