Python efficiently split currency sign and number in one string

前端 未结 6 955
一生所求
一生所求 2021-01-16 07:04

I have a string like \'$200,000,000\' or \'Yan300,000,000\'

I want to split the currency and number, and output a tuple (\'$\', \'200

6条回答
  •  梦谈多话
    2021-01-16 07:17

    >>> filter(str.isdigit, s)
    '200000000'
    >>> filter(lambda x: not x.isdigit() and x != ',', s)
    '$'
    >>> 
    >>> (filter(lambda x: not x.isdigit() and x != ',' ,s), filter(str.isdigit, s))
    ('$', '200000000')
    >>> 
    

提交回复
热议问题