Python 拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/436599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Split String
提问by
Lets Say we have Zaptoit:685158:[email protected]
假设我们有 Zaptoit:685158:[email protected]
How do you split so it only be left 685158:[email protected]
怎么分才只剩下 685158:[email protected]
回答by Graeme Perrow
>>> s = 'Zaptoit:685158:[email protected]'
>>> s.split( ':', 1 )[1]
'685158:[email protected]'
回答by Federico A. Ramponi
回答by Jay
Another method, without using split:
另一种方法,不使用拆分:
s = 'Zaptoit:685158:[email protected]'
s[s.find(':')+1:]
Ex:
前任:
>>> s = 'Zaptoit:685158:[email protected]'
>>> s[s.find(':')+1:]
'685158:[email protected]'
回答by Bryce
As of Python 2.5 there is an even more direct solution. It degrades nicely if the separator is not found:
从 Python 2.5 开始,有一个更直接的解决方案。如果找不到分隔符,它会很好地降级:
>>> s = 'Zaptoit:685158:[email protected]'
>>> s.partition(':')
('Zaptoit', ':', '685158:[email protected]')
>>> s.partition(':')[2]
'685158:[email protected]'
>>> s.partition(';')
('Zaptoit:685158:[email protected]', '', '')
回答by rnso
Following splits the string, ignores first element and rejoins the rest:
以下拆分字符串,忽略第一个元素并重新加入其余元素:
":".join(x.split(":")[1:])
Output:
输出:
'685158:[email protected]'
回答by Arnab Ghosal
Use the method str.split() with the value of maxsplit argument as 1.
使用方法 str.split() 并将 maxsplit 参数的值设为 1。
mailID = 'Zaptoit:685158:[email protected]'
mailID.split(':', 1)[1]
Hope it helped.
希望它有所帮助。
回答by PEZ
s = re.sub('^.*?:', '', s)