1

I have some Python code that pulls strings out of the crwaling output. My code so far:

 import requests, json, bs4, csv, re
 import urllib

 response = urllib.request.urlopen('https://currency-api.appspot.com/api/USD/EUR.json')
 jsondata = json.loads(response.read().decode("utf-8"))
 Wechselkurs = (jsondata['rate'])


 jsonUrl = "https://www.jsox.de/s/results.json?&q=London& customerSearch=1&page=0
 response = session.get(jsonUrl, headers=headers)
 js_dict = (json.loads(response.content.decode('utf-8')))

 for item in js_dict:
 prices = js_dict['searchResults']["tours"]

     for price in zip(prices):
         price_final = price.get("price")["original"]
         if price_final:
            price_end = int(float(price_final)*100*Wechselkurs)
            print(price_end)

This gives an error:

price_end = int(float(price_final)*100*Wechselkurs)
ValueError: could not convert string to float: '27,44\xa0€'

Why can't '27,44\xa0€'be converted to a float? I guess because of the fact that I have

 \xa
  €

in my float which prevents parsing.

Can you guys help me out? Any feedback is appreciated

4
  • Are you looking to convert 27,44.. into 27.44? You won't be able to convert the comma into a float either. Commented Apr 22, 2017 at 19:18
  • The \xa0 is a non-breaking space. Using re, you may remove all non-digits at the end and replace the comma with a period - price_final = re.sub(r'\D+$', '', price_final.replace(",", ".")) Commented Apr 22, 2017 at 19:21
  • @Wiktor: How can i remove any extra commas with your method? I´m getting the folllowing error: ValueError: could not convert string to float: '1.264.71' Commented Apr 22, 2017 at 21:02
  • And what is the value you need? 1.264 or 1.26471 or any other? Are there any safe criteria to follow here? Looks like the input you have is messy. Commented Apr 22, 2017 at 21:09

1 Answer 1

4
...
# thanks for the suggestion @RobertSeaman
price_final = price_final[::-1].replace('.', ',').replace(',', '.', 1)[::-1].translate(None, "\xa0€,")
price_end = int(float(price_final) * 100 * Wechselkurs)
...

https://docs.python.org/2/library/string.html#string.translate

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks, It is almost working. Getting the correct results but at one point I´m getting the following error: ValueError: could not convert string to float: '1.264.71'
You can turn the string around, replace the first , to a ., and then turn it back around. Then you can remove any extra commas: price_final = price_final[::-1].replace(',', '.', 1)[::-1].translate(None, "\xa0€,")
@RobertSeaman and m0dem Thanks for your feedback:) Stuck into another problem: price_final = price_final[::-1].replace(',', '.', 1)[::-1].translate(None, "\xa0€,") TypeError: translate() takes exactly one argument (2 given) Any suggestions here?
@SeriousRuffy What version of python are you using? You could try .translate(None, delete="\xa0€,")
As discussed, it turns out the source value was 1.097,68, therefore, the suggested workaround was to replace all . with ,. This brings the command to: price_final = price_final[::-1].replace('.', ',').replace(',', '.', 1)[::-1].translate(None, "\xa0€,")
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.