Python学习笔记(四)

Python学习笔记(四)

  1. 作业讲解
  2. 编码和解码

1. 作业讲解

  • 重复代码瘦身

# 定义地图
nav = {'省略'}

# 现在所处的层
current_layer = nav
# 记录你去过的地方
parent_list = []
# 是否结束循环
not_quit = True

while not_quit:
    for i in current_layer:
        print(i)
    print("输入对应项进入 | 输入 b 返回上一层 | 输入 q 退出")
    choice = input(">>>:").strip()
    if len(choice) == 0:    continue
    if choice in current_layer:
        parent_list.append(current_layer)
        current_layer = current_layer[choice]
    elif choice == 'q':
        not_quit = False
    elif choice == 'b':
        if len(parent_list)<1:
            print("您已在最顶层!")
        else:
            current_layer = parent_list.pop()
    else:
        print("无此项!")

2. 编码和解码

  • 图解编码和解码

编码解码图解

  1. Python2中的编码和解码
  • 默认ASCII
# -*- coding:utf-8 -*-

s = "这是汉语"
s_to_unicode = s.decode("utf-8")
unicode_to_gbk = s_to_unicode.encode("gbk")
print(s)				# 结果:乱码
print(s_to_unicode)		# 结果:这是汉语
print(unicode_to_gbk)	# 结果:这是汉语

  1. Python3中的编码和解码
  • 默认 Unicode

# 在python3中encode的过程会将数据转成byte类型
# 在decode解码的过程中会将byte转成字符串

以此为趣,乐在其中。
原文地址:https://www.cnblogs.com/ryomahan/p/7506549.html