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

posted on 2015-06-26 14:55  蒋乐兴的技术随笔  阅读(236)  评论(0编辑  收藏  举报

导航