In Python How To Strip Dollar Signs And Commas From Dollar Related Fields Only
Solution 1:
Unless you are really tied to the idea of using a regex, I would suggest doing something simple, straight-forward, and generally easy to read:
defconvert_money(inval):
if inval[0] == '$':
test_val = inval[1:].replace(",", "")
try:
_ = float(test_val)
except:
passelse:
inval = test_val
return inval
defconvert_string(s):
return"|".join(map(convert_money, s.split("|")))
a = '$1,000|hi,you|$45.43'
b = '$300.03|$MS2|$55,000'print convert_string(a)
print convert_string(b)
OUTPUT
1000|hi,you|45.43
300.03|$MS2|55000
Solution 2:
A simple approach:
>>>import re>>>exp = '\$\d+(,|\.)?\d+'>>>s = '$1,000|hi,you|$45.43'>>>'|'.join(i.translate(None, '$,') if re.match(exp, i) else i for i in s.split('|'))
'1000|hi,you|45.43'
Solution 3:
It sounds like you are addressing the entire line of text at once. I think your first task would be to break up your string by columns into an array or some other variables. Once you've don that, your solution for converting strings of currency into numbers doesn't have to worry about the other fields.
Once you've done that, I think there is probably an easier way to do this task than with regular expressions. You could start with this SO question.
If you really want to use regex though, then this pattern should work for you:
\[$,]\g
Replace matches with empty strings. The pattern gets a little more complicated if you have other kinds of currency present.
Solution 4:
Solution 5:
Use the regexx
((?<=\d),(?=\d))|(\$(?=\d))
eg
import re
>>> x="$1,000|hi,you|$45.43"
re.sub( r'((?<=\d),(?=\d))|(\$(?=\d))', r'', x)
'1000|hi,you|45.43'
Post a Comment for "In Python How To Strip Dollar Signs And Commas From Dollar Related Fields Only"