python编码解码,字符数据转换问题(自学笔记)

# 编码问题,字符转化问题
text = input('请输入待处理的数据:')
print(text)
# 将数据unicode编码一下,解码用decode就行了
text1 = text.encode("utf-8")
print(text1)
# 将输入的数据(包括数字,字母,中文等)转换成二进制
text2 = ' '.join(format(ord(x), 'b') for x in text)
print(text2)
# 将二进制转化为原数据
text3 = ''.join([chr(i) for i in [int(b, 2) for b in text2.split(' ')]])
print(text3)

运行结果:

 有需要补充的欢迎留言啊,这只是我个人遇到的问题,做的小总结,欢迎评论讨论。

本文来自博客园,作者:Jaoany,转载请注明原文链接:https://www.cnblogs.com/fanglijiao/p/15044982.html

原文地址:https://www.cnblogs.com/fanglijiao/p/15044982.html