同步操作将从 Cacho/PythonProject-PythonLession 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# -!- coding: utf-8 -!-'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''#作者:cacho_37967865#博客:https://blog.csdn.net/sinat_37967865#文件:python_encode.py#日期:2019年11月29日#备注:Python的编码示例'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''str_en = 'I love computer'str_cn = '我喜欢 computer'bytes_e = b'I love computer'bytes_c = b'\xe6\x88\x91\xe5\x96\x9c\xe6\xac\xa2 computer'#bytes_cm = b'我喜欢 computer' # bytes can only contain ASCII literal characters.# bytes方法进行编码def bytes_sample():bytes_en_by = bytes(str_en, encoding='utf_8')bytes_cn_by = bytes(str_cn, encoding='utf_8')print('纯英文字符串转换为bytes:',bytes_en_by)print('含中文字符串转换为bytes:',bytes_cn_by)# 编码def encode_sample():bytes_en = str_en.encode('utf-8')bytes_cn = str_cn.encode('utf-8')bytes_en_as = str_en.encode('ascii')print('utf-8纯英文编码:',bytes_en)print('utf-8含中文编码:',bytes_cn)print('ascii纯英文编码:',bytes_en_as)#str_bytes= str.encode(str_cn) # 默认utf-8,等价于下面3个#str_bytes= str.encode(str_cn,'utf_8')#str_bytes= str.encode(str_cn,encoding='utf_8')str_bytes= str_cn.encode()print('str转成by含中文:',str_bytes)#bytes_cn_as = str_cn.encode('ascii')#print('ascii含中文编码,会报错:',bytes_cn_as)# 解码def decode_sample():str_e = bytes_e.decode('utf-8')str_c = bytes_c.decode('utf-8')str_a = bytes_e.decode('ascii')print('utf-8纯英文解码:',str_e)print('utf-8含中文解码:',str_c)print('ascii纯英文解码:',str_a)if __name__ == '__main__':encode_sample()#decode_sample()#bytes_sample()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。