python - String to float and regex -
i have problem regex , string...
i need solution in regex code take float number of string. don't know why code doesn't work.
from bs4 import beautifulsoup import urllib2 re import sub url = 'http://www.ebay.es/itm/pet-shop-boys-official-promo-barcelona-electric-tour-beer-cerveza-20cl-bottle-/111116266655' #raw_input('dime la url que deseas: ') code = urllib2.urlopen(url).read(); soup = beautifulsoup(code) info = soup.find('span', id='v4-27').contents[0] print info info = sub("[\d]+,+[\d]", "", info) = float(info) print
\d
means non-digits. need use \d
instead. here details: http://en.wikipedia.org/wiki/regular_expression#character_classes
updated
i see, approach replace non-digits chars. mind, match needed information more clear:
>>> import re >>> s = "15,00 eur" >>> price_string = re.search('(\d+,\d+)', s).group(1) >>> price_string '15,00' >>> float(price_string.replace(',', '.')) 15.0
Comments
Post a Comment