【问题标题】:Changing encoding in csv file through python UTF-8 to UTF-16通过 python UTF-8 将 csv 文件中的编码更改为 UTF-16
【发布时间】:2014-11-25 14:40:26
【问题描述】:

如何通过 python 脚本更改编码?

我有一些文件正在循环执行其他操作。但在此之前,我需要将每个文件的编码从 UTF-8 更改为 UTF-16,因为 SQL Server 不支持 UTF-8

试过这个,但不工作。

data = "UTF-8 data"
udata = data.decode("utf-8")
data = udata.encode("utf-16","ignore")

干杯!

【问题讨论】:

    标签: python python-2.7 encoding utf-8 utf-16


    【解决方案1】:

    如果您想将文件从 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 2011-01-19
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多