How to read unicode from a text file

Read unicode text file


#!/usr/bin/env python# from   w  ww  .jav  a  2s. co m

CODEC = 'utf-8'
FILE = 'unicode.txt'

hello_out = u"Hello world\n"
bytes_out = hello_out.encode(CODEC)
f = open(FILE, "w")
f.write(bytes_out)
f.close()

f = open(FILE, "r")
bytes_in = f.read()
f.close()
hello_in = bytes_in.decode(CODEC)
print hello_in,

The code above generates the following result.





















Home »
  Python »
    Advanced Features »




Exception Handling
File
Module