python2.7写入文件时指定编码为utf-8

python3.0可以这样写
f = open('ufile.log', 'w', 'utf-8')
 
但在python2.7中open()没有编码参数,如上那样写会报错,可以使用如下模块
import codecs    #这个模块可以实现。
s = '中文;113456789876543234567'
f = codecs.open('ufile.log', 'w', 'utf-8')
f.write(s.decode('utf-8'))
f.close()
 

---------------------------------------------------------------------------------

关注微信公众号即可在手机上查阅,并可接收更多测试分享~

原文地址:https://www.cnblogs.com/songzhenhua/p/9312717.html