python --中文相关问题

往文件中写入中文,再从文件中读出,范例1:

#coding:utf-8
with open('data.log','w') as f:
    a=u'中文'
    b = a.encode('gbk')
    f.write(b)

with open('data.log','r') as f:
    l1= f.readlines()
    print l1[0]

写入中文,范例2:

#-*- coding:gbk-*-
file='test.txt'
a=u'中文'
b=a.encode('utf')
f=open(file,"w")
f.write(b)
f.close()
print "successful" 

 

posted on 2015-12-14 11:15  清明-心若淡定  阅读(115)  评论(0编辑  收藏  举报