#coding=utf-8
try:
JAP=open("e:/jap.txt","r")
CHN=open("e:/chn.txt","r")
UTF=open("e:/utf.txt","w")
jap_text=JAP.readline()
chn_text=CHN.readline()
#先decode成UTF-16,再encode成UTF-8
jap_text_utf8=jap_text.decode("SHIFT_JIS").encode("UTF-8") #不转成utf-8也可以
chn_text_utf8=chn_text.decode("GB2312").encode("UTF-8")#编码方式大小写都行utf-8也一样
UTF.write(jap_text_utf8)
UTF.write(chn_text_utf8)
except IOError,e:
print "open file error",e
四、Tk库支持ascii,UTF-16,UTF-8
代码如下:
#coding=utf-8
from Tkinter import *
try:
JAP=open("e:/jap.txt","r")
str1=JAP.readline()
except IOError,e:
print "open file error",e
root=Tk()
label1=Label(root,text=str1.decode("SHIFT_JIS")) #如果没有decode则显示乱码
label1.grid()
root.mainloop()
以上是学习python处理python编码的基本过程,希望对大家有帮助。