回答編集履歴
1
ユニコードエラーについて追記
answer
CHANGED
@@ -5,4 +5,12 @@
|
|
5
5
|
with open('text.sjis.dat', 'w') as f:
|
6
6
|
for line in open('text.dat'):
|
7
7
|
f.write(unicode(line, 'utf-8').encode('shift-jis'))
|
8
|
+
```
|
9
|
+
|
10
|
+
追記:ところで Shift-JIS は難しい漢字や特殊な記号に対応していません。それらを変換しようとすると、UnicodeEncodeError が発生します。これを回避するには error モードを replace (`?`に置換) か ignore (無視してスキップ) にします。
|
11
|
+
|
12
|
+
```python
|
13
|
+
unicode(line, 'utf-8').encode('shift-jis')
|
14
|
+
↓
|
15
|
+
unicode(line, 'utf-8').encode('shift-jis', errors='ignore')
|
8
16
|
```
|