Issue
I have a string like '$200,000,000'
or 'Yan300,000,000'
I want to split the currency and number, and output a tuple ('$', '200000000')
, without ','
in the number string.
Currently I’m using the following script, which is working:
def splitCurrency(cur_str):
cuttingIdx = 0
for char in cur_str:
try:
int(char)
break
except ValueError:
cuttingIdx = cuttingIdx + 1
return (cur_str[0:cuttingIdx].strip(),
cur_str[cuttingIdx:len(cur_str)].replace(',',''))
I want to avoid using for-loop and try-except for performance and readability. Any suggestions?
Solution
>>> import re
>>> string = 'YAN300,000,000'
>>> match = re.search(r'([\D]+)([\d,]+)', string)
>>> output = (match.group(1), match.group(2).replace(',',''))
>>> output
('YAN', '300000000')
Answered By – CTKlein
Answer Checked By – David Goodson (BugsFixing Volunteer)