forked from SilentInt/font-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gb2312.py
35 lines (31 loc) · 1.17 KB
/
gb2312.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
31
32
33
34
35
def print_gb2312_characters():
content = ""
# ASCII characters
for k in range(32, 127):
char_ascii = chr(k)
content += char_ascii
# print(char_ascii, end='\n')
for i in range(0xA1, 0xF8):
for j in range(0xA1, 0xFF):
# 避免遇到一些无效的编码
if (i == 0xAA and j == 0xA1) or (i == 0xBA and j == 0xA1) or (i == 0xD7 and j == 0xFA):
continue
# 使用gb2312编码解码
char_bytes = bytes([i, j])
try:
# 判断是否为ASCII字符
if char_bytes[0] < 0x80 and char_bytes[1] < 0x80:
char_ascii = char_bytes.decode('ascii')
content += char_ascii
# print(char_ascii, end='\n')
else:
char_gb2312 = char_bytes.decode('gb2312')
content += char_gb2312
# print(char_gb2312, end='\n')
except UnicodeDecodeError:
pass
return content
if __name__ == "__main__":
res = print_gb2312_characters()
with open('char.txt', 'w', encoding='utf-8') as f:
f.write(res)