字串格式转换

问题

Arthur Wong <[email protected]>
reply-to        [email protected]
to      [email protected]
date    Fri, Feb 6, 2009 at 17:05
subject [CPyUG:78239] 字符串转换问题

假如当前字符串是:2009-02-06 17:02:48.024179 如何能转换成:20090206170248024179 (就是仅保留数字部分) ?

Leo Jay:列表推导+str.isdigit

Leo Jay <[email protected]>
reply-to        [email protected]
to      [email protected]
date    Fri, Feb 6, 2009 at 17:08
subject [CPyUG:78241] Re: 字符串转换问题

>>> a = '2009-02-06 17:02:48.024179'
>>> b = ''.join([c for c in a if c.isdigit()])
>>> b
'20090206170248024179'
>>>

Samuel Chi:filter()+str.isdigit

Samuel Chi <[email protected]>
reply-to        [email protected]
to      [email protected]
date    Fri, Feb 6, 2009 at 18:01
subject [CPyUG:78250] Re: 字符串转换问题
mailing list    <python-cn.googlegroups.com> Filter messages from this mailing list
mailed-by       googlegroups.com
signed-by       googlegroups.com

扩展一下

filter(str.isdigit, '2009-02-06 17:02:48.024179') 


反馈

创建 by -- ZoomQuiet [2009-02-06 12:43:37]

Name Password4deL ;) :( X-( B-)

PageCommentData