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用于去除右边的字符.这三个函数都可传入一个参数,指定要去除的首尾字符.注意的是,传入的是一个字符数 ...
随机推荐
- Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务
Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具:Spr ...
- 学习shader之前必须知道的东西之计算机图形学(一)渲染管线
引言 shader到底是干什么用的?shader的工作原理是什么? 其实当我们对这个问题还很懵懂的时候,就已经开始急不可耐的要四处搜寻有关shader的资料,恨不得立刻上手写一个出来.但看了一些资料甚 ...
- 【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1685 由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证 ...
- 【BZOJ】1660: [Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1660 单调栈裸题..累计比每一个点高的个数即可. #include <cstdio> # ...
- java 问题
1. 在ezmorph包中 有个 引用类时 写法为import [Z; 为什么加个[看不懂
- when case group by 的用法集合
1.用那个以前大家都熟悉的例子,要求是依旧下面的表格求每个大洲的人口总和 国家(countrcoungry) 人口(population) 中国 600 美国 100 加拿大 100 英国 200 法 ...
- hdu 1348:Wall(计算几何,求凸包周长)
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Spring_day01--注入对象类型属性(重点)_P名称空间注入_注入复杂类型属性_IOC和DI区别_Spring整合web项目原理
注入对象类型属性(重点) Action要new一个service对象,Service中又要new一个Dao对象,现在把new的过程交给spring来操作 1 创建service类和dao类 (1)在s ...
- 13个非常实用的JavaScript小技巧
使用!!操作符转换布尔值 有时候我们需要对一个变量查检其是否存在或者检查值是否有一个有效值,如果存在就返回true值.为了做这样的验证,我们可以使用!!操作符来实现是非常的方便与简单.对于变量可以使用 ...
- SQL 使用序列
SQL 使用序列 序列是根据需要产生的一组有序整数:1, 2, 3 ... 序列在数据库中经常用到,因为许多应用要求数据表中的的每一行都有一个唯一的值,序列为此提供了一种简单的方法. 本节阐述在 My ...