-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_charset_1.py
More file actions
executable file
·90 lines (67 loc) · 2.79 KB
/
Copy pathpython_charset_1.py
File metadata and controls
executable file
·90 lines (67 loc) · 2.79 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
# -*- utf-8 -*-
#
# 23 June, 2017, Suzhou, PRC
# Karl.Lv@outlook.com, KarlLv@126.com
#
# Python characters encoding and decoding
# Both traditional and simplified Chinese characters
#
import sys
print("----------------------------------------------------------------------")
print("----------------------------------------------------------------------")
print("----------------------------------------------------------------------")
print("------------- Python characters encoding and decoding ------------")
print("----------------------------------------------------------------------")
print("----------------------------------------------------------------------")
print("----------------------------------------------------------------------")
print("sys.getdefaultencoding(): ", sys.getdefaultencoding())
print("----------------------------------------------------------------------")
###############################################################################
# traditional Chinese
###############################################################################
print("---- traditional Chinese ----")
str_cht_1 = '三皇五帝 夏商周 春秋戰國 秦漢 隋唐 元明清'
print(str_cht_1)
print("---- Gig5 ----")
print(str_cht_1.encode('big5'))
print("---- CP950 ----")
print(str_cht_1.encode('cp950'))
print("---- GB18030 ----")
print(str_cht_1.encode('gb18030'))
print("---- UTF-8 ----")
print(str_cht_1.encode('utf-8'))
###############################################################################
# simplified Chinese
###############################################################################
print("---- simplified Chinese ----")
str_chs_1 = '三皇五帝 夏商周 春秋战国 秦汉 隋唐 元明清'
print(str_chs_1)
print("---- GB2132 ----")
print(str_chs_1.encode('gb2312'))
print("---- GBK ----")
print(str_chs_1.encode('gbk'))
print("---- CP936 ----")
print(str_chs_1.encode('cp936'))
print("---- GB18030 ----")
print(str_chs_1.encode('gb18030'))
print("---- UTF-8 ----")
print(str_chs_1.encode('utf-8'))
#print(ord('人'))
#print(hex(ord('人')))
###############################################################################
###############################################################################
print("----------------------------------------------------------------------")
print("Display Unicode of some Chinese characters")
print("----------------------------------------------------------------------")
print(str_chs_1)
for x in str_chs_1:
print(hex(ord(x)), end='')
print()
print("More rare Chinese characters")
str_chs_2 = '叒 叕 𡦪 𩺰 龖 𪙹 抟 鹪 鹩 呺 洴 澼 絖 斄 炎 焱 燚 爨'
print(str_chs_2)
for x in str_chs_2:
print(hex(ord(x)), end='')
print()
print(''.join(list(map(lambda x:hex(ord(x)), str_chs_2))))