##language:zh #pragma section-numbers off ##含有章节索引导航的 ZPyUG 文章通用模板 <> ## 默许导航,请保留 <> = 字串格式转换 = ##startInc == 问题 == {{{ Arthur Wong reply-to python-cn@googlegroups.com to python-cn@googlegroups.com 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 reply-to python-cn@googlegroups.com to python-cn@googlegroups.com 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 reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Fri, Feb 6, 2009 at 18:01 subject [CPyUG:78250] Re: 字符串转换问题 mailing list Filter messages from this mailing list mailed-by googlegroups.com signed-by googlegroups.com }}} 扩展一下 {{{ filter(str.isdigit, '2009-02-06 17:02:48.024179') }}} ##endInc ---- '''反馈'''