質問するログイン新規登録

回答編集履歴

1

説明を改善

2021/12/26 08:38

投稿

cx20
cx20

スコア4700

answer CHANGED
@@ -7,40 +7,60 @@
7
7
 
8
8
  漢字として認識できないコード(NULL文字等)も含まれている為、ダメそうな気がします。
9
9
 
10
- 下記に Python で下記のようなサンプルがありました。文字一覧を取得したいのであれば、こちらが参考になるのでは、と思います。
10
+ ~~下記に Python で下記のようなサンプルがありました。文字一覧を取得したいのであれば、こちらが参考になるのでは、と思います。
11
+ ~~
12
+ ~~■ Unicodeの中のJIS X 0208に当たる文字を取得~~
13
+ ~~[https://nakamura001.hatenablog.com/entry/20110108/1294453032](https://nakamura001.hatenablog.com/entry/20110108/1294453032)~~
11
14
 
15
+ ~~といっても、~~
12
- Unicodeの中のJIS X 0208に当たる文字を取得
16
+ ~~Shift-JIS / JIS X 0208文字コード / Unicode の文字コード が列挙されたファイル~~
13
- [https://nakamura001.hatenablog.com/entry/20110108/1294453032](https://nakamura001.hatenablog.com/entry/20110108/1294453032)
17
+ ~~[http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0208.TXT](http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0208.TXT)~~
18
+ ~~から、対象のコードをファイル出力しているようですけど。~~
14
19
 
15
- といっても、
16
- Shift-JIS / JIS X 0208 の文字コード / Unicode 文字コード が列挙されファイ
20
+ ~~Shift-JIS の文字の一覧を出力するように修正しサンプはこちらになります。~~
17
- [http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0208.TXT](http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0208.TXT)
18
- から、対象のコードをファイル出力しているようですけど。
19
21
 
22
+ <追記>
20
- Shift-JIS 文字の一覧を出力するように修正したサンプルはこちらにます。
23
+ 上記のサンプル CP932 の情報が取得できい為、回答内容を修正させて頂きます。
21
- ```Python
22
- import codecs
23
- dec=codecs.getdecoder('shift-jis')
24
- ch_list = []
25
- f = open('JIS0208.TXT', 'r')
26
- for row in f:
27
- if row[0] != '#':
28
- c = row.split("\t")[0]
29
- ch_list.append(dec(int(c, 16).to_bytes(2, byteorder="big"))[0])
30
- f.close()
31
24
 
32
- f = codecs.open('sjis.txt', 'w', 'shift-jis')
33
- txt = "".join(ch_list)
25
+ 「Shift JIS」と「CP932」について改めて確認してみました。
34
- f.write(txt)
26
+ Python における日本語の文字コード指定としては以下のコード指定が可能です。
35
- f.close()
36
27
 
37
- ```
28
+ ・cp932
29
+ ・shift_jis
30
+ ・shift_jisx0213
31
+ ・shift_jis_2004
38
32
 
39
- CP932 の文字一覧を取得する方法は、ちょっと調べないと分からないです。
40
- Shift JIS と CP932 の違については下記が参考になるかとす。
33
+ 詳し説明は下記にありますが、大き違いとしては機種依存文字が使えるか否か、ということのようです。
41
34
 
42
- <参考>
43
- ■ CP932とMS932の違いを調べて知ったCP932とSJISの違い
44
- [https://ponsuke-tarou.hatenablog.com/entry/2020/10/08/002458](https://ponsuke-tarou.hatenablog.com/entry/2020/10/08/002458)
45
35
  ■ Python♪Windowsの「Shift JIS」の落とし穴
46
- [https://snowtree-injune.com/2020/05/15/codec-py003/](https://snowtree-injune.com/2020/05/15/codec-py003/)
36
+ [https://snowtree-injune.com/2020/05/15/codec-py003/](https://snowtree-injune.com/2020/05/15/codec-py003/)
37
+
38
+ 質問者さんが確認したい「Shift JIS」というのは機種依存文字が使える「CP932」のことではないでしょうか?
39
+ ■ "shift_jis" と "cp932" の比較結果
40
+ ![イメージ説明](2ebcc129166f0da72e440c734e040def.png)
41
+
42
+ 質問者さんのコードを少しだけ修正してみました。
43
+ `isprintable()` を用いて、出力可能な文字のみを表示するようにしてみました。
44
+ こんな感じでいかがでしょうか?
45
+
46
+ ```python
47
+ import codecs
48
+ dec=codecs.getdecoder('cp932')
49
+ for i in range(256):
50
+ try:
51
+ test=dec(bytes([i]))[0]
52
+ if len(test)==1 and test.isprintable():
53
+ print(test)
54
+ except:
55
+ pass
56
+
57
+ for i in range(256):
58
+ for j in range(256):
59
+ try:
60
+ test=dec(bytes([i,j]))[0]
61
+ if len(test)==1 and test.isprintable():
62
+ print(test)
63
+ except:
64
+ pass
65
+
66
+ ```