Convert encoding of a csv file from utf-8 to shift-jis in python

2.1k Views Asked by At

I have a CSV file with utf-8 encoding. I want to change it's to shift-jis csv file using python code. Is it possible? How can i do it?

1

There are 1 best solutions below

2
Daweo On

This sounds like task for codecs (it is part of standard library). Two codecs.open might be used if you want to just change encoding following way

import codecs
with codecs.open("file1.csv","r",encoding="utf_8") as fin:
    with codecs.open("file2.csv","w",encoding="shift_jis") as fout:
        fout.write(fin.read())

above code assumes that you have UTF-8 encoded file file1.csv and what to create shitf-jis encoded file2.csv and you have enough RAM space free to load whole file there. Be warned that in Standard Encodings following shift_jis encoding are available

  • shift_jis
  • shift_jis_2004
  • shift_jisx0213

I do not know difference between them, so you would need yourself which one you actually need to use.