python 文件里面的编码问题

py36
with as 打开的
()里面可以直接加上第三个参数,encoding=‘utf-8’

py27
在f.write里面的东西后面使用.encode(‘utf-8’)

 with open(os.path.join(paths,'r2', 
            os.path.splitext(os.path.basename(ima))[0])+ ".txt", "w", encoding='utf-8') as f:
            f.write("The time is:{}s".format(time.time() - t)+"\n")
            for key in result:
#                f.write(result[key][1].encode("utf-8")) ##py27
                f.write(result[key][1])
                f.write("\n")

[Refer-CB](https://www.cnblogs.com/baojun2014/p/9085533.html)

发布了70 篇原创文章 · 获赞 0 · 访问量 1705

猜你喜欢

转载自blog.csdn.net/weixin_44807751/article/details/105000666