How to convert hex string into Japanese and write it to the file in Python 2.7?
<p>I am trying to convert a hex string into Japanese(codec: SHIFT-JIS) and write the Japanese output to a file using Python 2.7. However, all I have got is the original hex string in the file. Can someone tell me where I did wrong? Here is the code I use:</p>

<pre><code>fd = open(path,'w')
temp_str ='\x8d\xc5\x82\xe0\x8d\x82\x8b\x4d\x82\xc8\x89\xa4\x82\xc5\x82\xa0\x82\xc1\x82\xbd\x82\xbc\x81\x76\x80\x01\xff\xff'
fd.write(temp_str.encode('shift-jis'))
fd.close()
</code></pre>

<p>All I have got in the file is "\x8d\xc5\x82\xe0\x8d\x82\x8b\x4d\x82\xc8\x89\xa4\x82\xc5\x82\xa0\x82\xc1\x82\xbd\x82\xbc\x81\x76\x80\x01\xff\xff". </p>

<p>Maybe you should open file in "wb" instead "w" mode?</p>

python python-2.7