使用Python去除字符串中某个字符的多种实现方式比较
作者:AllardZhao 时间:2023-07-15 00:45:13
1、如何去掉字符串中不需要的字符?
实际案例:
(1)过滤掉用户输入前后多余的空白字符:' nick2008@gmail.com '
(2)过滤某windows下编辑文本中的'\r':'hello world\r\n'
(3)去掉文本中的unicode组合符号(调音):u'ní hǎo, chī fàn'(或 èěéēàǎā)
解决方案;
方法1:字符串strip(),lstrip(),rstrip()方法去掉字符串两端字符。
方法2:删除单个固定位置的字符,可以使用切片+拼接的方式。
方法3:字符串的replace()替换方法或正则表达式re.sub()删除任意位置字符。(通用)
方法4:字符串translate()方法,可以同时删除多种不同字符。
2、代码演示
# 方法1:strip类的方法
# 去掉字符串两端的字符
s = ' abc 123 '
'''
str.strip方法介绍:
strip([chars]) -> string or unicode
chars不指定默认情况下去掉空白字符(空格,\n,\t,\r)
'''
# 去掉两端空白,但保留了中间的空白
print(s.strip())
# 只去掉左端的空白
print(s.lstrip())
# 只去掉右端的空白
print(s.rstrip())
s2 = '---abc+++'
# 去掉s2中的加减
print(s2.strip('-+'))
# 方法2:删除固定位置字符,切片+拼接
s3 = 'abc:123'
# 只删除固定位置的冒号
s4 = s3[0:3] + s3[4:]
print(s4)
# 方法3:
'''
因为第1种方法不能删除中间的某些字符,只能在两端进行删除,
第3种就能完成删除中间某些字符的事情。
'''
s5 = '\tabc\t123\txyz'
# 清除s5中的所有\t,可以使用字符串替换replace,
# 但是这种方式只能替换一种
print(s5.replace('\t', ''))
s6 = '\tabc\t123\txyz\ropq\r'
import re
# 去除s6中的\t和\r
print(re.sub('[\t\r]', '', s6))
# 方法4:
'''
str.translate方法介绍:
S.translate(table) -> string
table如果是None不做任何映射,如果存在就是一个字符映射到另一个字符上去的表;
'''
s7 = 'abc1230323xyz'
# 现在对s7字符串进行加密,加密规则是将其中a全部替换成x,
# b替换成y,c替换成z,反过来将其中的xyz分别替换成abc
# 构建映射表
make = str.maketrans('abcxyz', 'xyzabc')
print(make)
# 对s7字符串进行加密转换
print(s7.translate(make))
s8 = 'abc\refg\n2342\t'
# 删除s8中\r,\n,\t,构建映射表str_trans
str_trans = str.maketrans('', '', '\t\r\n')
print(s8.translate(str_trans))
# 去掉音标符号
u = u'nǐ hǎo, chī fàn'
import unicodedata, sys
# 将原始输入标准化为分解形式字符
a = unicodedata.normalize('NFD', u)
'''
使用dict.fromkeys() 方法构造一个字典,每个Unicode和音调作为键,对应的值全部为None
sys.maxunicode : 给出最大Unicode代码点的值的整数,即1114111(十六进制的0x10FFFF)。
unicodedata.combining:将分配给字符chr的规范组合类作为整数返回。如果未定义组合类,则返回0
这样我们就成功将所有组合类的值全部设置为None
'''
cmb_chrs = dict.fromkeys(c for c in range(sys.maxunicode)
if unicodedata.combining(chr(c)))
# 调用translate函数删除所有音调
print(a.translate(cmb_chrs))
来源:https://blog.csdn.net/qq_37189082/article/details/124546253
标签:python,去除,字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 使用 PyMysql、DBUtils 创建连接池提升性能
2024-01-26 05:49:49
![](https://img.aspxhome.com/file/2023/4/87994_0s.png)
JS实现密码框根据焦点的获取与失去控制文字的消失与显示效果
2024-04-10 10:50:20
详解pandas.DataFrame中删除包涵特定字符串所在的行
2023-08-23 23:37:45
![](https://img.aspxhome.com/file/2023/6/76966_0s.png)
zend framework文件上传功能实例代码
2024-05-05 09:17:17
MySql 5.7.17压缩包免安装的配置过程图解
2024-01-27 20:02:14
![](https://img.aspxhome.com/file/2023/7/108877_0s.png)
如何用ASP输出HTML文件?
2010-06-11 20:01:00
如何解决ASP脚本运行超时的错误
2007-10-14 11:38:00
完美解决Python2操作中文名文件乱码的问题
2022-12-12 11:29:46
![](https://img.aspxhome.com/file/2023/2/90832_0s.jpg)
python 将numpy维度不同的数组相加相乘操作
2022-03-26 06:14:06
![](https://img.aspxhome.com/file/2023/5/93425_0s.jpg)
两行代码实现的QQ窗口抖动效果
2008-09-06 12:34:00
go variant底层原理深入解析
2024-05-22 17:45:08
![](https://img.aspxhome.com/file/2023/0/125100_0s.png)
python3 cookbook中常遇问题解答
2022-09-07 09:07:05
python 多维切片之冒号和三个点的用法介绍
2023-05-05 11:57:23
![](https://img.aspxhome.com/file/2023/4/124934_0s.jpg)
Vue父子组建的简单通信之控制开关Switch的实现
2024-06-05 09:16:25
![](https://img.aspxhome.com/file/2023/8/123058_0s.gif)
带你了解python装饰器
2023-08-26 23:10:57
Django中的ajax请求
2022-10-19 10:28:14
![](https://img.aspxhome.com/file/2023/5/99035_0s.png)
windows mysql 自动备份的几种方法汇总
2024-01-25 05:44:43
Python 从attribute到property详解
2022-03-15 18:52:40
![](https://img.aspxhome.com/file/2023/9/112619_0s.jpg)
Python中的 enumerate和zip详情
2022-10-22 23:48:30
![](https://img.aspxhome.com/file/2023/8/107118_0s.png)
在Python反编译中批量pyc转 py的实现代码
2023-08-11 20:21:04
![](https://img.aspxhome.com/file/2023/2/61732_0s.png)