Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I ran few commands like recode and iconv but none worked. I think doing this task via Python will surely help.

What should be the appropriate approach?

like image 720
aviral sanjay Avatar asked Feb 27 '26 04:02

aviral sanjay


2 Answers

import the libraries

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)

NB: make sure to read the file with the correct encoding in this case utf-16 as specified in the question and then write it to an output file using the utf-8 encoding sequence.

like image 75
IBRAHIM ALI MUSAH Avatar answered Mar 01 '26 18:03

IBRAHIM ALI MUSAH


import codecs
import shutil

with codecs.open("input_file.utf8.csv", encoding="utf-8") as input_file:
    with codecs.open(
            "output_file.utf16.csv", "w", encoding="utf-16") as output_file:
        shutil.copyfileobj(input_file, output_file)
like image 30
Ori Arbes Avatar answered Mar 01 '26 17:03

Ori Arbes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!