Python核心编程笔记--unicode编码

#定义编码方式,与物理文件。

code='utf-8'
file='D:/utf8.txt'

#把编码后的字符写入文件。

hello_out =u'hello world'
bytes_out =hello_out.encode(code)

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(code)

print hello_in

原文地址:https://www.cnblogs.com/JiangLe/p/4602268.html