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
'\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的更多相关文章
- Python strip lstrip rstrip使用方法(字符串处理空格)
Python strip lstrip rstrip使用方法(字符串处理空格) strip是trim掉字符串两边的空格.lstrip, trim掉左边的空格rstrip, trim掉右边的空格 s ...
- python strip() lstrip() rstrip() 使用方法
Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除最左边的字符,rstrip用于去除最右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入 ...
- 【转】Python中string的strip,lstrip,rstrip用法
Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是 ...
- ython strip lstrip rstrip使用方法
Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是一 ...
- python之字符串strip、rstrip、lstrip的方法
1.描述 strip():用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列 rstrip():用于移除字符串右边指定的字符(默认为空格或换行符)或字符序列 lstrip():用于移除字符串 ...
- unicodedata.normalize()/使用strip()、rstrip()和lstrip()/encode和decode 笔记(具体可看 《Python Cookbook》3rd Edition 2.9~2.11)
unicodedata.normalize()清理字符串 # normalize()的第一个参数指定字符串标准化的方式,分别有NFD/NFC >>> s1 = 'Spicy Jala ...
- python 知识 rstrip,strip,lstrip
rstrip,strip,lstrip 作用:去除字符串中的空格或指定字符 一.默认用法:去除空格str.strip() : 去除字符串两边的空格str.lstrip() : 去除字符串左边的空格s ...
- Python中的strip()的理解
在看到Python中strip的时候产生了疑问 strip() 用于移除字符串头尾指定的字符(默认为空格) 开始测试: >>> s = 'ncy_123.python' >&g ...
- python3----strip lstrip rstrip
Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符.这三个函数都可传入一个参数,指定要去除的首尾字符.注意的是,传入的是一个字符数 ...
随机推荐
- UDP也需要现有Server端,然后再有Client端
UDP编程: DatagramSocket(邮递员):对应数据报的Socket概念,不需要创建两个socket,不可使用输入输出流. DatagramPacket(信件):数据包,是UDP下进行传输数 ...
- 【BZOJ】1068: [SCOI2007]压缩(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1068 发现如果只设一维的话无法转移 那么我们开第二维,发现对于前i个来说,如果确定了M在哪里,第i个 ...
- Cocos2d-x-Lua (2.x)脚本开发之 Lua语言基础
从今天開始,往后将陆续更新Lua教程,主要是搭载Cocos2dx ,有不论什么疑惑或者不正确的地方.尽情指正.交流.探讨. 那么首先肯定是Lua语言基础的知识点.以下直接附上代码,凝视已经非常清楚.无 ...
- 清除信号量队列导致zabbix自动关闭
前几天在海外UCloud机器上部署了一套zabbix proxy和zabbix agentd,可是第二天一大早就收到邮件说zabbix_proxy挂掉了,上去查一下发现两台机器中的一台的proxy和a ...
- hdu 1756:Cupid's Arrow(计算几何,判断点在多边形内)
Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- C# 压缩和解压文件(SharpZipLib)
先从网上下载ICSharpCode.SharpZipLib.dll类库 将文件或文件夹压缩为zip,函数如下 /// <summary> /// 压缩文件 /// </summary ...
- 如何用ChemDraw中的ChemFinder查询反应过程
ChemFinder是ChemDraw化学绘图软件的重要插件之一,ChemFinder是一个贮存众多化学信息的数据库管理系统,不仅可以用于查询基本化学结构,用户还可以用ChemFinder查询需要的反 ...
- jhipster(springboot+datatable+jpa)后台分页,总结
最近用datatable做了一个后台分页,但是后台实体原本没写DTO.就碰到的问题做了一下总结 一.datatable使用get方式传数据到后台,这是正常的后台分页,不涉及过滤查询和前端传递的排序字段 ...
- C语言程序真正的启动函数
为什么要用”真正”这个词?因为我们从学C语言开始,都会先明白这个道理,即C语言有且仅有一个main函数,main函数是C语言的入口点和出口点!(可以参考>http://www.dotcpp.co ...
- poj_1836 动态规划
题目大意 N个士兵排成一排,不是按照高度顺序排列.现在想要从中去掉几名士兵,从而使得队伍中剩余的士兵能够看到这排最左边或者最右边的那个士兵,某士兵能够看到最左边(或最右边)的士兵指这名士兵和最左边(或 ...