这种情况,需要增加进行编码,如下:
with open('aa.txt','wb') as f:
f.write(s.encode('UTF-8'))
或者,可以使用下面的方法:
try:
with open(mainlog_filename,'r') as fp:
for line in fp.readlines():
print line.decode('gb2312').encode('utf-8')
except Exception as e :
print e