3

Hi there i am coding in python trying to make a user friendly currency exchange app for a school project but have encountered and error with trying to decode json for the exchange rates. The code i'm using is:

import urllib.request
import json
(str) = "http://rate-exchange.appspot.com/currency?from=FRM&to=TO&q=AM";
(str) = (str.replace("FRM", "GBP"))
(str) = (str.replace("TO", "USD"))
url = (str.replace("AM", "20"))
f = urllib.request.urlopen(url)
data = (f.read(100))
print (data)
json_input = data
decoded = json.loads(json_input)
print ("conversion is: ", decoded["v"])

and the error that i am getting is:

b'{"to": "USD", "rate": 1.66215, "from": "GBP", "v": 33.243000000000002}'
Traceback (most recent call last):
File "C:\Users\jay\My Cubby\get qure.py", line 12, in <module>
decoded = json.loads(json_input)
File "C:\Python33\lib\json\__init__.py", line 309, in loads
return _default_decoder.decode(s)
File "C:\Python33\lib\json\decoder.py", line 352, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: can't use a string pattern on a bytes-like object

So i was just wondering if anyone had any ideas of how to fix this error? Or if anyone has seen this error before? Thanks in advance for any help J.Rymer

0

1 Answer 1

12

In Python 3, you need to decode the bytes return value from urllib.request.urlopen() to a unicode string:

decoded = json.loads(json_input.decode('utf8'))

This makes the assumption that the web service you are using is using the default JSON encoding of UTF-8.

You could check the response for a character set if you don't want to assume:

f = urllib.request.urlopen(url)
charset = f.info().get_param('charset', 'utf8')
data = f.read()
decoded = json.loads(data.decode(charset))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick responce! And it works 100% so thanks :)
The first decoded code snippet worked for me in django-rest-framework testing. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.