如何通过python脚本更改编码?
我有一些文件可以循环播放其他内容。但在此之前,我需要将每个文件的编码从UTF-8更改为UTF-16,因为SQL Server不支持UTF-8
试过这个,但不行。
data = "UTF-8 data"
udata = data.decode("utf-8")
data = udata.encode("utf-16","ignore")
干杯!
答案 0 :(得分:2)
如果要将utf-8编码的文件转换为utf-16编码的文件,则此脚本有效:
#!/usr/bin/python2.7
import codecs
import shutil
with codecs.open("input_file.utf8.txt", encoding="utf-8") as input_file:
with codecs.open(
"output_file.utf16.txt", "w", encoding="utf-16") as output_file:
shutil.copyfileobj(input_file, output_file)