Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #extract #price #string
ADD COMMENT
Topic
Name
8+6 =