在python中保存文件时添加BOM(unicode签名) [英] Adding BOM (unicode signature) while saving file in python

查看:73
本文介绍了在python中保存文件时添加BOM(unicode签名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python中保存文件时添加BOM(unicode签名):

How can I add BOM (unicode signature) while saving file in python:

file_old = open('old.txt', mode='r', encoding='utf-8')
file_new = open('new.txt', mode='w', encoding='utf-16-le')
file_new.write(file_old.read())

我需要将文件转换为utf-16-le + BOM.现在脚本运行良好,只是没有 BOM.

I need to convert file to utf-16-le + BOM. Now script is working great, except that there is no BOM.

推荐答案

直接写在文件开头:

file_new.write('\ufeff')

这篇关于在python中保存文件时添加BOM(unicode签名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆