Ansi to UTF-8 using python causing error

后端 未结 1 1746
时光说笑
时光说笑 2020-12-19 13:38

While I was trying to write a python program that converts Ansi to UTF-8, I found this

https://stackoverflow.com/questions/14732996/how-can-i-convert-utf-8-to-ansi-i

相关标签:
1条回答
  • 2020-12-19 13:58

    You are trying to use the Python 3 version of the open() function, on Python 2. Between the major versions, I/O support was overhauled, supporting better encoding and decoding.

    You can get the same new version in Python 2 as io.open() instead.

    I'd use the shutil.copyfileobj() function to do the copying, so you don't have to read the whole file into memory:

    import io
    import shutil
    
    with io.open(file_path_ansi, encoding='latin-1', errors='ignore') as source:
        with io.open(file_path_utf8, mode='w', encoding='utf-8') as target:
            shutil.copyfileobj(source, target)
    

    Be careful though; most people talking about ANSI refer to one of the Windows codepages; you may really have a file in CP (codepage) 1252, which is almost, but not quite the same thing as ISO-8859-1 (Latin 1). If so, use cp1252 instead of latin-1 as the encoding parameter.

    0 讨论(0)
提交回复
热议问题