python3- 编码格式

常见编码:

ASCII:单字节 GB2312:简体中文编码集 GBK:兼容扩展了GB2312,能显示繁体中文,能显示日文中的片假名。 Unicode:国际组织制定的可以容纳世界上所有文字和符号的字符编码方案。每个字符占用2个字节。 UTF-8:是最流行的一种对 Unicode 进行传播和存储的编码方式。可变长度,比如英文字符和数字占1个字节,汉字占3个字节。 现在普遍应用:本机存储用Unicode,网络传输用utf-8

python解释器中:

python2 默认编码格式为 ascii python3 默认编码格式为Unicode

(下面对于python编码设计的方面和查看系统编码格式来自博客:http://www.cnblogs.com/fnng/p/5008884.html)

在开发python程序过程中,会涉及到三个方面的编码:

python程序的编码 python程序运行环境的编码 python程序读取外部文件、网页的编码

python程序的编码:

首行加:# -*- coding: utf-8 -*-

python程序运行时环境(IDE)的编码:

执行下面一段代码:

  1. #!/usr/bin/env python

  2. # -*- coding: utf-8 -*-

  3.  

  4. #python3 代码,wing ware 5 编辑

  5.  

  6. name = "你好"

  7. print(name)


在Windows cmd下执行时,出现下面问题:

  因为Windows cmd的编码默认是 936(cmd框顶部右键——属性——选项——当前代码页),而python代码中编码格式为utf-8。
在cmd中输入:chcp 65001 回车执行(Windows cmd下,65001 表示utf-8),cmd顶部,右键——属性——字体设置为 Lucida Console,再来执行python程序就正常了。
(把字体重新设置为点阵字体,cmd中输入:chcp 936 回车执行,cmd回到原来设置了)

python程序读取外部文件、网页的编码:

暂时没找到合适的例子

查看python系统编码:

(python2 方法一样)

编码格式的转变:decode()与encode()

encode():把Unicode编码格式的字符串转换为其他编码格式的字符串 decode(): 将其他编码的字符串换成Unicode编码的字符串

代码:

  1. #!/usr/bin/env python

  2. # -*- coding: utf-8 -*-

  3.  

  4. #python3 代码,wing ware 5 编辑

  5.  

  6. #ord("单个字符") 查看字符对应的编码

  7. print("字符a的编码为:",ord("a"))

  8.  

  9. #chr(整数n) 查看整数n 对应的字符

  10. print("编码97对应的字符为:",chr(97))

  11.  

  12. #查看单个汉字对应的编码

  13. print("汉字你对应的编码为:",ord("你"))

  14.  

  15. #查看编码对应的汉字

  16. print("编码20320对应的字为:",chr(20320))

  17.  

  18. #python3默认的编码格式是Unicode

  19. #encode("编码格式a") 把Unicode编码格式转换为 编码格式a

  20. #decode("编码格式a") 把编码格式为 编码格式a  的字符串 转换为编码格式为Unicode的字符串

  21.  

  22. str1 = "你好啊"

  23. # encode("utf-8") 把Unicode编码格式转为utf-8编码格式

  24. print("{_str1} 转换为utf-8编码格式后为:".format(_str1=str1),str1.encode("utf-8"))

  25. str2 = "hello"

  26. print("{_str2} 转换为utf-8编码格式后为:".format(_str2=str2),str2.encode("utf-8"))

  27. str3 = "hello你好啊"

  28. print("{_str3} 转换为utf-8编码格式后为:".format(_str3=str3),str3.encode("utf-8"))

  29.  

  30. # encode("utf-8") 把utf-8格式转为Unicode

  31. str4 = str3.encode("utf-8")

  32. print("str4为:",str4)

  33. print("编码格式为utf-8的str4,转换为编码格式Unicode后为:",str4.decode("utf-8"))

  34.  

  35. # 网络数据传输时会用到这两种转换

运行结果:

[python] < class="ViewSource" style="box-sizing: border-box;outline: 0px;color: rgb(103, 149, 181);cursor: pointer;background-image: url("https://mmbiz.qpic.cn/mmbiz_png/0Sl9RuzzEs7u6PiaKiclwouPU1tpH56x2bNIOnUNh3WmicKgv1cux1ycn87ONJicKgj9sFBRR3slMq9YvwDolbxqvA/640?wx_fmt=gif");background-position: left top;background-size: initial;background-repeat: no-repeat;background-attachment: initial;background-origin: initial;background-clip: initial;background-color: inherit;border-width: initial;border-style: none;border-color: initial;padding: 1px;margin-right: 10px;word-break: break-all;font-size: 9px;display: inline-block;width: 16px;height: 16px;text-indent: -2000px;" title="view plain">view plain < class="CopyToClipboard" style="box-sizing: border-box;outline: 0px;color: rgb(103, 149, 181);cursor: pointer;background-image: url("https://mmbiz.qpic.cn/mmbiz_png/0Sl9RuzzEs7u6PiaKiclwouPU1tpH56x2bXemZMnKaX6Lz1wiaNnMv0uoicLL69oIwy8STpaT3oXpJJLzJ6ia4ghH2A/640?wx_fmt=gif");background-position: left top;background-size: initial;background-repeat: no-repeat;background-attachment: initial;background-origin: initial;background-clip: initial;background-color: inherit;border-width: initial;border-style: none;border-color: initial;padding: 1px;margin-right: 10px;word-break: break-all;font-size: 9px;display: inline-block;width: 16px;height: 16px;text-indent: -2000px;" title="copy">copy

  1.   

  2. 字符a的编码为: 97

  3. 编码97对应的字符为: a

  4. 汉字你对应的编码为: 20320

  5. 编码20320对应的字为: 你

  6. 你好啊 转换为utf-8编码格式后为: b'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a'

  7. hello 转换为utf-8编码格式后为: b'hello'

  8. hello你好啊 转换为utf-8编码格式后为: b'hello\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a'

  9. str4为: b'hello\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a'

  10. 编码格式为utf-8的str4,转换为编码格式Unicode后为: hello你好啊

  11.  

更多学员笔记请加歪歪老师QQ2675336290

课程咨询请加妞妞老师QQ2474123456

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值