JSON does not return utf-8

2

I'm using freegeoip to know my location and use it in my program, but when I make a call using:

r = requests.get(location_url, headers={"content-type":"application/json;charset=UTF-8"})

The return looks like this:

{
  ip: "0.0.0.0",
  country_code: "BR",
  country_name: "Brazil",
  region_code: "SP",
  region_name: "Sao Paulo",
  city: "São Paulo",
  zip_code: "",
  time_zone: "America/Sao_Paulo",
  latitude: -20.1323,
  longitude: -50.6417,
  metro_code: 0
}

When I read using r.text , I need accents in words. How can I do this?

    
asked by anonymous 10.07.2017 / 18:10

3 answers

2

Try decoding the answer manually:

r = requests.get(location_url)
texto = r.content.decode('utf8')

Note: r.json() can also be useful in your case.

    
10.07.2017 / 18:57
0

Instead of using UTF-8 try using ISO-8859-1 :

r = requests.get(location_url, headers={"content-type":"application/json;charset=ISO-8859-1"})
    
10.07.2017 / 18:28
0

Instead of UTF-8 try to use latin1 . I do not know why, but I had the same problem and solved it.

    
12.07.2017 / 18:20