Reputation: 39
I'm trying to create a simple script that shows me the currency value between BRL x USD, I got to the part where I get the value, but not as a float variable which I need for future conversion operations.
How do I get only the numbers in the brlxusd
variable? Right now it prints out "0.253059 USD", I just want the "0.253059" part.
import urllib
from bs4 import BeautifulSoup
def currencyValues():
url = urllib.urlopen('http://www.x-rates.com/calculator/?from=BRL&to=USD&amount=1')
soup = BeautifulSoup(url, "html.parser")
for usdbrl in soup.find_all("span", class_="ccOutputRslt"):
brlxusd = usdbrl.text
print "BRL x USD rate today: %s" % brlxusd
currencyValues()
Upvotes: 3
Views: 73
Reputation: 55448
How do I get only the numbers in the brlxusd variable? Right now it prints out "0.253059 USD", I just want the "0.253059" part.
>>> brlxusd = '0.253059 USD'
>>> brlxusd.split()[0]
'0.253059'
Upvotes: 0
Reputation: 456
A fast and simple way is split the string in brlxusd and get the float part separated from USD part. your code inside for loop will be:
brlxusd = usdbrl.text
brlxusd = brlxusd.split(" ")[0]
# if you want to convert it to float use float(brlxusd.split(" ")[0])
print "BRL x USD rate today: %s" % brlxusd
This is assuming that all data will be in same format as you stated.
Upvotes: 2