0

I have csv files that I need to edit in Python that have to remain in Shift-JIS. When test my code by entering each section into the Python interpreter, the files get edited fine and they stay in Shift-JIS. I run the following lines in the Python interpreter:

import sys, codecs
reload(sys)
sys.setdefaultencoding('shift_jis')

I put these lines in a script and run them from the DOS prompt and of course the shift-JIS characters I add get messed up. If I run chcp at the DOS prompt, it tells me that I'm running chcp 932, shift-JIS. Does anybody know what's not working?

Tensigh
  • 1,030
  • 5
  • 22
  • 44

1 Answers1

1

In case anyone needs to know, this is the remedy:

In this case Python was using Unicode when I needed Shift-JIS. What worked for me was specifying lines to use unicode then encode them in Shift-JIS, then write them to the file. This worked every time.

For example:

name = u"ใƒ†ใ‚นใƒˆ "
newstring = name + other_string_data
newstring = newstring.encode('shift_jis')

Then the string would get encoded into shift-JIS and written. This isn't the most elegant way to do this but I hope this helps somebody, it took me about 2 hours to figure out.

Tensigh
  • 1,030
  • 5
  • 22
  • 44