-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-encoding.py
30 lines (25 loc) · 929 Bytes
/
08-encoding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# See also: http://kunststube.net/encoding/
from pathlib import Path
import sqlite3
fname = Path(__file__).with_suffix('.txt')
# We can read the binary version, which may or may not be helpful
with open(fname, 'rb') as fo:
contents = fo.read().strip()
print(type(contents))
print(contents)
# Default encoding (made explicit) is utf-8, but that won't work here:
with open(fname, 'r', encoding='utf-8', errors='replace') as fo:
contents = fo.read().strip()
print(type(contents))
print(contents)
# The thing about encoding is, you can guess, but even when you are wrong, it may not always throw
# an error:
with open(fname, 'r', encoding='macroman') as fo:
contents = fo.read().strip()
print(type(contents))
print(contents)
# This is actually what we intended
with open(fname, 'r', encoding='shift_jis') as fo:
contents = fo.read().strip()
print(type(contents))
print(contents)