Answers for "python - extract the price from a string"

0

python - extract the price from a string

# Visit: https://regexr.com/
# and look at the Menu/Cheatsheet

# Extract the price from a string
price = 'Price is: $520,130.250'
expr = 'Price is: \$([0-9,]*\.[0-9]*)'

match = re.search(expr, price)
print(match.group(0))              # give entire match
print(match.group(1))              # give only text in brackets

price_without_comma = match.group(1).replace(',', '')     # replace comma because can't be converted to a number
price_num = float(price_without_comma)                    # convert string to float 
print(price_num)
Posted by: Guest on August-13-2020

Code answers related to "python - extract the price from a string"

Python Answers by Framework

Browse Popular Code Answers by Language