Encoding, conversion bytes - strings

1

I have a problem, I've been searching but all the solutions I find do not work. The problem is that I am accessing a page (.txt) and I can not convert it from bytes to string , in order to work the data (ex: page.split("\n") )

import urllib.request

def open_url(url):

   data = urllib.request.urlopen(url);
   page = data.read()
   return page

def Main():

   url = "http://openweathermap.org/help/city_list.txt"
   page = open_url(url)

   print(page)

Main()

So far so good, the page is returned and printed in bytes , what I would like now is to convert it to string , already tried:

>
print(page.decode('utf-8'))

But it gives an error:

  

UnicodeDecodeError: 'utf-8' codec can not decode byte 0x96 in position 289664: invalid start byte

All other solutions I've seen are equivalent to this one, they may change a bit to syntax but I think they do the same, eg page.decode(encoding='UTF-8') , the error that gives is the same as described above.

I would like to know a way to get around this and make this into string .

    
asked by anonymous 02.07.2015 / 12:41

1 answer

1

The code worked normally here, but you can try to give the print like this:

print(str(page, 'iso-8859-1'))

Here it worked both ways.

    
20.09.2015 / 04:46