The first part is day4- three login experiment, character encoding.

Keywords: Python encoding ascii Spring Windows

# Three login experiments
 1 memu = {
 2     "Shaanxi":{
 3         "Xi'an City":{
 4             "New urban area":["Daming Palace Site","Hanyuan Temple Site","Eighth Route Army Office"],
 5             "Forest of steles":["Anbeilin Museum","Ming Dynasty City Wall","Little wild goose pagoda" ],
 6             "Lianhu District":["bell tower and drum tower","Ming Dynasty City Wall","Xi'an's capital Temple""Grand Mosque"],
 7             "Yanta District":["Qin II Tomb of Hu Hai","Daxingshan Temple","Big Wild Goose Pagoda","Qu Jiang Pool","Shaanxi History Museum"],
 8             "Weiyang District":["Qin al Fang Palace","Han Weiyang Palace","Tang Daming Palace"],
 9             "Xie":["Hua Xu's hometown","Shui Lu an","Shun Shan Wang","Tang Yu","Ruins of man"]
10         },
11         "Market":{
12             "Nanzheng":["Red Temple Lake Scenic Area","Liping national Forest Park","Hanshan Plaza"],
13             "Chenggu":["Zhang Qian Memorial Hall","Lake Scenic Area","Orange garden scenic area"],
14             "Yangxian County":["Kaiyuan Shrita"],
15             "Foping":["Daping Island Scenic Area","Foping County Nature Reserve"]
16         }
17     },
18     "Chongqing City":{
19         "Yuzhong District":["Monument for Liberation","Chaotianmen","Hongya cave","Joseph Hall"],
20         "District":["Wanzhou falls","qinglong waterfall","Born City","Tiefeng mountain country"],
21         "District":["White crane beam","Big wood Flower Valley","Wuling Mountain Rift Valley","816 Nuclear military cavities"],
22         "Dadukou":["Xiaonanhai Hot Spring","China Virtue Park","Golden Cake Dream Kingdom"],
23         "Jiangbei District":["Guanyin Bridge","Ping Ping","Fang Fang science fiction Park"]
24     }
25 }
 1 current_layer = memu
 2 parent_layer = []
 3 while True:
 4     for key in current_layer:
 5         print(key)
 6     choice = input(">>>:").strip()
 7     if len(choice) == 0 : continue
 8     if choice in current_layer:
 9         parent_layer.append(current_layer)
10         try:
11             if current_layer[choice]:
12                 current_layer = current_layer[choice]
13         except Exception:
14             print("Input error,Please re-enter!".center(50,'-'))
15             continue
16     elif choice == 'b':
17         try:
18             if current_layer:
19                 current_layer = parent_layer.pop()
20         except Exception:
21             print("Has returned to the home page directory".center(50,'-'))
22 
23     else:
24         print("No such item")
Character coding --

The development of Chinese character encoding --
ASCII: can only store English and Latin characters, one character takes up one character, 8 bits.
The GB2312:GB 2312 standard contains 6763 Chinese characters.
GBK:GBK has 21886 Chinese characters and graphic symbols.
GB18030:GB 18030 is compatible with GB 2312-1980 and GBK, and 70244 Chinese characters are included.

unicode:utf-32: A character takes up four bytes
unicode:utf-16: A character takes up 2 bytes or more
unicode:utf-8: an English is stored in ASCII code, a Chinese account for 3 bytes.

#-----in python2-----
UTF-8 --decode-->Unicode--encode-->GBK
GBK--decode-->Unicode--encode-->UTF-8

The test environment is python2, running on Windows client (GBK code).
1 a = "Fantastic"
2 gbk_to_unicode = a.decode("utf-8")
3 print(a) #Hamlet
4 utf8_to_unicode = gbk_to_unicode.encode("gbk")
5 print(gbk_to_unicode) #Fantastic
6 print(utf8_to_unicode)#Fantastic
#-----in python3 -----
Character default encoding Unicode, file default encoding UTF-8
encode will transform data into bytes type at the same time of encoding.
decode converts data into strings while decoding
b = byte = character type = [0-255]


Posted by reflash on Wed, 02 Oct 2019 03:04:47 -0700