Python efficiently split currency sign and number in one string

前端 未结 6 883
一生所求
一生所求 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

    >>> import re
    >>> string = 'YAN300,000,000'
    >>> match = re.search(r'([\D]+)([\d,]+)', string)
    >>> output = (match.group(1), match.group(2).replace(',',''))
    >>> output
    ('YAN', '300000000')
    

    (Thanks to zhangyangyu for pointing out I hadn't fully answered the question)

提交回复
热议问题