Question : How can I decode UTF characters from a file using Python?

Hello,

I understand that Python cannot read or write UTF characters by default and I have managed to resolve this using the code in line 1 of the code section.

I would now like to know the simpliest way to decode these characters, given the simple function for reading files in the code section?

Many thanks
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
filetowrite.write(message.encode("utf-8"))
 
 
def readfile(readfilename):
    openfile = open(readfilename, "r")
    file_contents = openfile.read()
    #print file_contents
    openfile.close()
    return file_contents
Open in New Window Select All

Answer : How can I decode UTF characters from a file using Python?

If you use the codecs module, you can use a wrapped version of a file object that transparently encodes when writing and decodes when reading.
1:
2:
3:
4:
5:
6:
7:
8:
9:
import codecs
 
 
def readfile(readfilename):
    openfile = codecs.open(readfilename, "r", "utf-8")
    file_contents = openfile.read() # This is a unicode string
	print file_contents
    openfile.close()
    return file_contents
Open in New Window Select All
Random Solutions  
 
programming4us programming4us