【问题标题】:Convert encoding of a text file from utf-8 to ansi or unicode in python将文本文件的编码从 utf-8 转换为 python 中的 ansi 或 unicode
【发布时间】:2017-05-10 06:19:58
【问题描述】:

我有一个 utf-8 编码的文本文件。我想在 python 中自动将它的 unicode 更改为 ANSI 或 unicode。 可能吗? 我该怎么做?

【问题讨论】:

标签: python text encoding utf-8 ansi


【解决方案1】:

要将文件从 utf8 转换为 cp1252:

import io

with io.open(src_path, mode="r", encoding="utf8") as fd:
    content = fd.read()
with io.open(dst_path, mode="w", encoding="cp1252") as fd:
    fd.write(content)

【讨论】:

    【解决方案2】:

    试试这个

    #read input file
    with codecs.open('USERS.CSV', 'r', encoding = 'latin-1') as file:
    lines = file.read()  
    
    #write output file
    with codecs.open('1_UserPython.CSV', 'w', encoding = 'utf_8_sig') as file:
    file.write(lines)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      相关资源
      最近更新 更多