-1

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?

Gouri Mahapatra
  • 109
  • 1
  • 11

1 Answers1

0

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.

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • Daweo, Thanks. shift_jis_2004 and shift_jisx0213 working fine. however, some of the characters are incorrect. Like Hyphen not converted correctly UTF 8 date: 線-総司 Shift data : 線・総司 – Gouri Mahapatra Jun 15 '22 at 10:39
  • 1
    `codecs` is not needed. Built-in `open` has the same `encoding` parameter. – Mark Tolonen Jun 15 '22 at 12:42