4-6 如何去掉字符串中不需要的字符
方法一,字符串strip() lstrip() rstrip() 去掉字符串兩端字符
方法二,刪除單個位置的字符,可以使用切片 + 拼接的方式
方法三,字符串的replace()方法或者正則表達式re.sub刪除任意位置字符
方法四,字符串translate方法,可以同時刪除多種不同的字符
方法一,字符串strip() lstrip() rstrip() 去掉字符串兩端字符,默認參數是空格’ ‘,可以跟多個字符作爲參數
>>> s = '***abc***def***'
>>> s.strip('*') 去掉左右的*
'abc***def'
>>> s.lstrip('*') 去掉左邊的*
'abc***def***'
>>> s.rstrip('*') 去掉右邊的*
'***abc***def'
sa = '---*+****+++--abc--++*++def*+*++*----'
sa.strip('-+*') 將左右兩邊的(-+*)全部刪除
'abc--++*++def'
>>>
方法二,刪除單個位置的字符,可以使用切片 + 拼接的方式
>>> s = "abc:def"
>>> s2 = s[:3] + s[4:]
>>> s2
'abcdef'
方法三,字符串的replace()方法或者正則表達式re.sub刪除任意位置字符
通過replace需要調用多次才能最後替換完成
s = "***+++\tabc***---\tdef**---\t**8"
>>> s
'***+++\tabc***---\tdef**---\t**8'
>>> s.replace("*",'').replace('-','').replace('+','').replace('\t','')
'abcdef8'
通過re.sub可以將需要替換的字符全部放在pattern中,由於是枚舉多個字符,因此需要用[\*,\t,\-,\+] 或者 \* | \t | |- | \+
>>> re.sub(r"[\*,\t,\-,\+]","",s)
'abcdef8'
>>> re.sub("\*|\\t|-|\+","",s)
'abcdef8'
>>>
方法四,字符串translate方法,可以同時刪除多種不同的字符
>>> s = "zabc123xyza"
>>> s.translate(string.maketrans("abcxyz","xyzabc"))
'cxyz123abcx'
>>> s = "zabc123xyza"
>>> s.translate(None,"abc")
'z123xyz'
>>>
help(str.strip/lstrip/rstrip/str.translate/string.maketrans/re.sub/str.replace
>>> help(str.strip)
Help on method_descriptor:
strip(...)
S.strip([chars]) -> string or unicode
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.
If chars is unicode, S will be converted to unicode before stripping
(END)
>>> help(str.lstrip)
Help on method_descriptor:
lstrip(...)
S.lstrip([chars]) -> string or unicode
Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping
>>>
>>> help(str.rstrip)
Help on method_descriptor:
rstrip(...)
S.rstrip([chars]) -> string or unicode
Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping
>>>
>>> help(str.translate)
Help on method_descriptor:
translate(...)
S.translate(table [,deletechars]) -> string
Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256 or None.
If the table argument is None, no translation is applied and
the operation simply removes the characters in deletechars.
>>> help(string.maketrans)
Help on built-in function maketrans in module strop:
maketrans(...)
maketrans(frm, to) -> string
Return a translation table (a string of 256 bytes long)
suitable for use in string.translate. The strings frm and to
must be of the same length.
>>>
>>> help(re.sub)
Help on function sub in module re:
sub(pattern, repl, string, count=0, flags=0)
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.
>>>
>>>
>>> help(str.replace)
Help on method_descriptor:
replace(...)
S.replace(old, new[, count]) -> string
Return a copy of string S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
>>>