编码与解码

 1 # 编码
 2 with open("file1.txt", "wb") as f1:
 3     str1 = "1234ytr*&^%$v简单方便v的积分把对方那边"
 4     enStr1 = str1.encode("utf-8")
 5     print(enStr1)  # b'123456kjhgfvbn0x'
 6     f1.write(enStr1)
 7 
 8 # 解码
 9 with open("file1.txt", "rb") as f2:
10     str1 = f2.read()  # 字节类型
11     # 解码
12     str2 = str1.decode("utf-8")
13     print(str2)

 

posted @ 2019-07-29 20:20  巨菜的小鸟  阅读(94)  评论(0编辑  收藏  举报