3 python replace以及split用法
阿新 • • 發佈:2019-01-31
1 replace用法
a = "<SPK/>一年**九店<NPS/>**<NON/>"
help(a.replace)
Help on built-in function replace: 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.
a = a.replace("<","_")
a = a.replace(">","_")
print a
_SPK/_一年**九店_NPS/_**_NON/_
2 split用法
help(a.split)
Help on built-in function split: split(...) S.split([sep [,maxsplit]]) -> list of strings Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.
b=a.split("_")
print b
['', 'SPK/', '\xe4\xb8\x80\xe5\xb9\xb4**\xe4\xb9\x9d\xe5\xba\x97', 'NPS/', '**', 'NON/', '']
print b[0]