2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pythonでJIS X 0208をShift-JISやUTF-16に変換する

Last updated at Posted at 2020-07-26

あることでJISコードを日本語変換したくなったときのやり方が分からなかったのでメモ。

方法

公開されているJIS X 0208コードの変換表を用いて連想配列を作成する
下は対応表

http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0208.TXT
http://ash.jp/code/unitbl21.htm

プログラム

jis.py

import os
ar = []
with open('./JIS0208.TXT') as f:
    for t_line in f:
        # 先頭がコメントの部分は除く
        if t_line[0] != "#":
            sjis, jis, utf16 = os.path.basename(t_line).split('\t')[0:3]
            ar.append([jis, utf16])
# 例) ar['0x3B3D'] -> '0x8695'
# JIS -> UTF-16
ar = dict(ar)

実行するとき

JISコードの3B3Dから"蚕"を出力する
0x3b3dだとエラーが出るので、0x3B3Dにするため文字を大文字に変換する。

jis.py

print(chr(int(ar['0x'+'3b3d'.upper()], 16)))
terminal
$ python jis.py

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?