simple Japanese text fixes using python

I promised a mate Philip I would put this one up on the web, so I will try and make the code as painless as possible, sans pipes unixy stuff etc.
If you have ever handled Japanese text you may know that there are more than a few ways to interpret the code. One of my favorite ways to convert text is using python.
The following is a really simple way to convert from one bytecode to another, trust me there are a million ways to do this and you can easily adapt it to your server side web scripts or even add a nice gui. The main code is only one line the rest is just file IO.
Lets say I want to send out mail in iso-2022-jp format (one that most keitai seems to be able to read) and I have written the text on (god forbid) winblows and have copied it to my tmp dir as "source.tmp". well usually winblows will use shift-jis.
So the source is shift-jis and target is in iso-2022-jp.
open a command prompt (here I am using mac)
vi ./myprogname.py

#!/usr/bin/python
# open the files
In = open ( '/tmp/source.tmp' )
Out = open ( '/tmp/target.tmp', 'w' )
# do our stuff this should
Out.write(unicode(In.read(), 'shift-jis').encode('iso-2022-jp'))
# close the files
In.close()
Out.close()

then make the file executable:
chmod +x ./myprogname.py
run the bugger:
./myprogname.py
it should spit out the code to the file "target.tmp"

lets do the same for utf-8 to shift-jis (say mac to winblows)
same deal just different encodings:
#!/usr/bin/python
# open the files
In = open ( '/tmp/source.tmp' )
Out = open ( '/tmp/target.tmp', 'w' )
# do our stuff
Out.write(unicode(In.read(), 'utf-8').encode('shift-jis'))
# close the files
In.close()
Out.close()


I do recommend trying a few and mail them to different machines and phones so you can see what is happening. You can drop the file in a browser and choose the encodings to see whats happening.
And yes it does work for other languages, give it a go!