Python3 去掉|删除字符串中不需要的字符,空格,特殊字符

实际案例:
1. 过滤掉用户输入中前后多余的空白字符:
' nick2008@gmail.com'

  1. 过滤某windows下编辑文本中的'\r':
    'hello world\r\n'

3.去掉文本中的unicode组合符号(音调):
u'zhào'

解决方案:

方法一:字符串strip(),lstrip(),rstrip()方法去掉字符串两端字符.
方法二:删除单个固定位置的字符,可以使用切片+拼接的方式.
方法三:字符串的replace()方法或正则表达式re.sub()删除任意位置字符.
方法四:字符串translate()方法,可以同时删除多种不同字符.

字符串strip(),lstrip(),rstrip()方法去掉字符串两端字符.

Python
In [1]: s = ' nick2008@gmail.com' In [5]: s.<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/strip" title="View all posts in strip" target="_blank">strip</a></span>? Docstring: S.strip([chars]) -> str Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. Type: builtin_function_or_method In [2]: s.strip() Out[2]: 'nick2008@gmail.com' # strip() 去除字符串,两侧的空格 In [3]: s.<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/rstrip" title="View all posts in rstrip" target="_blank">rstrip</a></span>() Out[3]: ' nick2008@gmail.com' # 去除字符串右侧的空格 In [4]: s.<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/lstrip" title="View all posts in lstrip" target="_blank">lstrip</a></span>() Out[4]: 'nick2008@gmail.com' # 去除字符串左侧的空格 In [6]: s = "**8888&" In [8]: s.strip("*&") Out[8]: '8888'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
In [ 1 ] : s = '    nick2008@gmail.com'
 
In [ 5 ] : s . strip ?
Docstring :
S . strip ( [ chars ] ) -> str
 
Return a copy of the string S with leading and trailing
whitespace removed .
If chars is given and not None , remove characters in chars instead .
Type :        builtin_function_or_method
 
In [ 2 ] : s . strip ( )
Out [ 2 ] : 'nick2008@gmail.com'
 
# strip() 去除字符串,两侧的空格
In [ 3 ] : s . rstrip ( )
Out [ 3 ] : '    nick2008@gmail.com'
# 去除字符串右侧的空格
 
In [ 4 ] : s . lstrip ( )
Out [ 4 ] : 'nick2008@gmail.com'
# 去除字符串左侧的空格
 
In [ 6 ] : s = "**8888&"
 
In [ 8 ] : s . strip ( "*&" )
Out [ 8 ] : '8888'

删除单个固定位置的字符,可以使用切片+拼接的方式.

Python
In [9]: s = "abc:lll" In [10]: s[:3] Out[10]: 'abc' In [11]: s[4:] Out[11]: 'lll' In [12]: s[:3]+s[4:] Out[12]: 'abclll'
1
2
3
4
5
6
7
8
9
10
In [ 9 ] : s = "abc:lll"
 
In [ 10 ] : s [ : 3 ]
Out [ 10 ] : 'abc'
 
In [ 11 ] : s [ 4 : ]
Out [ 11 ] : 'lll'
 
In [ 12 ] : s [ : 3 ] + s [ 4 : ]
Out [ 12 ] : 'abclll'

字符串的replace()方法或正则表达式re.sub()删除任意位置字符.

Python
In [13]: s Out[13]: 'abc:lll' In [14]: s.replace(':','') Out[14]: 'abclll' # replace 只替换单一的字符 对于多个字符替换,我们可以使用re.sub In [21]: s ="\tsdfsdf\rsdfdsfd\r" In [22]: re.sub("[\t\r\s]",'',s) Out[22]: 'sdfsdfsdfdsfd' In [23]: re.sub? Signature: re.sub(pattern, repl, string, count=0, flags=0) Docstring: Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
In [ 13 ] : s
Out [ 13 ] : 'abc:lll'
 
In [ 14 ] : s . replace ( ':' , '' )
Out [ 14 ] : 'abclll'
 
# replace 只替换单一的字符
 
对于多个字符替换 ,我们可以使用 re . sub
 
In [ 21 ] : s = "\tsdfsdf\rsdfdsfd\r"
 
In [ 22 ] : re . sub ( "[\t\r\s]" , '' , s )
Out [ 22 ] : 'sdfsdfsdfdsfd'
 
In [ 23 ] : re . sub ?
Signature : re . sub ( pattern , repl , string , count = 0 , flags = 0 )
Docstring :
Return the string obtained by replacing the leftmost
non - overlapping occurrences of the pattern in string by the
replacement repl .    repl can be either a string or a callable ;
if a string , backslash escapes in it are processed .    If it is
a callable , it' s passed the match object and must return
a replacement string to be used .


字符串translate()方法,可以同时删除多种不同字符.

Python maketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。
注:两个字符串的长度必须相同,为一一对应的关系

Python
In [32]: s Out[32]: 'abcoooooooxyz' In [33]: str.maketrans('abcxyz','xyzabc') Out[33]: {97: 120, 98: 121, 99: 122, 120: 97, 121: 98, 122: 99} # 注意 maketrans 在Python3 中不是通过 String 模块导入的,可以直接使用内置的函数str In [34]: s.<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/translate" title="View all posts in translate" target="_blank">translate</a></span>(str.maketrans('abcxyz','xyzabc')) Out[34]: 'xyzoooooooabc'
1
2
3
4
5
6
7
8
9
10
In [ 32 ] : s
Out [ 32 ] : 'abcoooooooxyz'
 
In [ 33 ] : str . maketrans ( 'abcxyz' , 'xyzabc' )
Out [ 33 ] : { 97 : 120 , 98 : 121 , 99 : 122 , 120 : 97 , 121 : 98 , 122 : 99 }
# 注意 maketrans 在Python3 中不是通过 String 模块导入的,可以直接使用内置的函数str
 
 
In [ 34 ] : s . translate ( str . maketrans ( 'abcxyz' , 'xyzabc' ) )
Out [ 34 ] : 'xyzoooooooabc'



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值