PDA

View Full Version : [Python3] Unicode conversion



ntanitime
March 9th, 2012, 10:03 AM
Hi,
which is the best way to convert a <class 'bytes'> in a <class 'str'>?

using "decode()"



import urllib.request
import urllib.error

def get_page( link ):
try:
f = urllib.request.urlopen( link )
except:
return False
else:
page = f.read().decode()
return page

F = get_page('http://www.wordreference.com/enit/ultimately')Or is better to use "str()" :



import urllib.request
import urllib.error

def get_page( link ):
try:
f = urllib.request.urlopen( link )
except:
return False
else:
page = f.read()
return str(page) #######

F = get_page('http://www.wordreference.com/enit/ultimately')

ofnuts
March 9th, 2012, 05:18 PM
str() has nothing to do with Unicode. It is just a printable version of the object. Use decode() with an explicit encoding.