Python – How to convert UTF-16 encoded csv file to UTF-8 using Python?

How to convert UTF-16 encoded csv file to UTF-8 using Python?… here is a solution to the problem.

How to convert UTF-16 encoded csv file to UTF-8 using Python?

I ran some commands like recode and iconv, but neither worked. I think doing this task through Python will definitely help.

What should be the appropriate method?

Solution

Import the library

import codecs
import shutil

with codecs.open("inputfile.csv", encoding="utf-16") as input_file:
with codecs.open(
        "outputfile.csv", "w", encoding="utf-8") as output_file:
    shutil.copyfileobj(input_file, output_file)

Note: Make sure to read the file using the correct encoding specified in the question (UTF-16 in this case) and then write it to the output file using the UTF-8 encoding sequence.

Related Problems and Solutions