Search
 
SCRIPT & CODE EXAMPLE
 

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)
Comment

PREVIOUS NEXT
Code Example
Python :: python import colors 
Python :: balancing paranthesis python 
Python :: how to find the cosine in python 
Python :: Python Tkinter PanedWindow Widget 
Python :: stegano python 
Python :: python sqrt function 
Python :: exponent in python 
Python :: np.exp in python numpy 
Python :: if condition in print statement python 
Python :: get operator as input in python 
Python :: comment out multiple lines in python 
Python :: python encoding utf 8 
Python :: check all true python 
Python :: django composite primary key 
Python :: odoo scaffold command 
Python :: python access modifiers 
Python :: push notification using python 
Python :: append string python 
Python :: remove word from string in python 
Python :: add horizontal line to plotly scatter 
Python :: twitter api tutorial python 
Python :: Python program to find N largest elements from a list 
Python :: python programm zu exe 
Python :: multiprocessing in jupyter notebook 
Python :: isnotin python 
Python :: flask on gevent over https 
Python :: pandas series remove element by index 
Python :: keras loss plot 
Python :: rename all columns 
Python :: any python 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =