回答編集履歴

1

説明を改善

2021/12/26 08:38

投稿

cx20
cx20

スコア4633

test CHANGED
@@ -16,76 +16,116 @@
16
16
 
17
17
 
18
18
 
19
- 下記に Python で下記のようなサンプルがありました。文字一覧を取得したいのであれば、こちらが参考になるのでは、と思います。
19
+ ~~下記に Python で下記のようなサンプルがありました。文字一覧を取得したいのであれば、こちらが参考になるのでは、と思います。
20
+
21
+ ~~
22
+
23
+ ~~■ Unicodeの中のJIS X 0208に当たる文字を取得~~
24
+
25
+ ~~[https://nakamura001.hatenablog.com/entry/20110108/1294453032](https://nakamura001.hatenablog.com/entry/20110108/1294453032)~~
20
26
 
21
27
 
22
28
 
23
- ■ Unicodeの中のJIS X 0208に当たる文字を取得
29
+ ~~といっても、~~
24
30
 
31
+ ~~■ Shift-JIS / JIS X 0208 の文字コード / Unicode の文字コード が列挙されたファイル~~
32
+
25
- [https://nakamura001.hatenablog.com/entry/20110108/1294453032](https://nakamura001.hatenablog.com/entry/20110108/1294453032)
33
+ ~~[http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0208.TXT](http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0208.TXT)~~
34
+
35
+ ~~から、対象のコードをファイル出力しているようですけど。~~
26
36
 
27
37
 
28
38
 
29
- といっても、
30
-
31
- Shift-JIS / JIS X 0208 の文字コード / Unicode 文字コード が列挙されファイ
39
+ ~~Shift-JIS の文字の一覧を出力するように修正しサンプはこちらになります。~~
32
-
33
- [http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0208.TXT](http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0208.TXT)
34
-
35
- から、対象のコードをファイル出力しているようですけど。
36
40
 
37
41
 
38
42
 
39
- Shift-JIS の文字の一覧を出力するように修正したサンプルはこちらになります。
43
+ <追記>
40
44
 
45
+ 上記のサンプルでは CP932 の情報が取得できない為、回答内容を修正させて頂きます。
46
+
47
+
48
+
49
+ 「Shift JIS」と「CP932」について改めて確認してみました。
50
+
51
+ Python における日本語の文字コード指定としては以下のコード指定が可能です。
52
+
53
+
54
+
55
+ ・cp932
56
+
57
+ ・shift_jis
58
+
59
+ ・shift_jisx0213
60
+
61
+ ・shift_jis_2004
62
+
63
+
64
+
65
+ 詳しい説明は下記にありますが、大きな違いとしては機種依存文字が使えるか否か、ということのようです。
66
+
67
+
68
+
69
+ ■ Python♪Windowsの「Shift JIS」の落とし穴
70
+
71
+ [https://snowtree-injune.com/2020/05/15/codec-py003/](https://snowtree-injune.com/2020/05/15/codec-py003/)
72
+
73
+
74
+
75
+ 質問者さんが確認したい「Shift JIS」というのは機種依存文字が使える「CP932」のことではないでしょうか?
76
+
77
+ ■ "shift_jis" と "cp932" の比較結果
78
+
79
+ ![イメージ説明](2ebcc129166f0da72e440c734e040def.png)
80
+
81
+
82
+
83
+ 質問者さんのコードを少しだけ修正してみました。
84
+
85
+ `isprintable()` を用いて、出力可能な文字のみを表示するようにしてみました。
86
+
87
+ こんな感じでいかがでしょうか?
88
+
89
+
90
+
41
- ```Python
91
+ ```python
42
92
 
43
93
  import codecs
44
94
 
45
- dec=codecs.getdecoder('shift-jis')
95
+ dec=codecs.getdecoder('cp932')
46
96
 
47
- ch_list = []
97
+ for i in range(256):
48
98
 
49
- f = open('JIS0208.TXT', 'r')
99
+ try:
50
100
 
51
- for row in f:
101
+ test=dec(bytes([i]))[0]
52
102
 
53
- if row[0] != '#':
103
+ if len(test)==1 and test.isprintable():
54
104
 
55
- c = row.split("\t")[0]
105
+ print(test)
56
106
 
57
- ch_list.append(dec(int(c, 16).to_bytes(2, byteorder="big"))[0])
107
+ except:
58
108
 
59
- f.close()
109
+ pass
60
110
 
61
111
 
62
112
 
63
- f = codecs.open('sjis.txt', 'w', 'shift-jis')
113
+ for i in range(256):
64
114
 
65
- txt = "".join(ch_list)
115
+ for j in range(256):
66
116
 
67
- f.write(txt)
117
+ try:
68
118
 
119
+ test=dec(bytes([i,j]))[0]
120
+
121
+ if len(test)==1 and test.isprintable():
122
+
69
- f.close()
123
+ print(test)
124
+
125
+ except:
126
+
127
+ pass
70
128
 
71
129
 
72
130
 
73
131
  ```
74
-
75
-
76
-
77
- CP932 の文字一覧を取得する方法は、ちょっと調べないと分からないです。
78
-
79
- Shift JIS と CP932 の違いについては下記が参考になるかと思います。
80
-
81
-
82
-
83
- <参考>
84
-
85
- ■ CP932とMS932の違いを調べて知ったCP932とSJISの違い
86
-
87
- [https://ponsuke-tarou.hatenablog.com/entry/2020/10/08/002458](https://ponsuke-tarou.hatenablog.com/entry/2020/10/08/002458)
88
-
89
- ■ Python♪Windowsの「Shift JIS」の落とし穴
90
-
91
- [https://snowtree-injune.com/2020/05/15/codec-py003/](https://snowtree-injune.com/2020/05/15/codec-py003/)