鱼C论坛

 找回密码
 立即注册
查看: 2208|回复: 10

[已解决]Python3的默认编码是什么

[复制链接]
发表于 2017-8-4 02:17:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
字符串默认编码是Unicode吗?
最佳答案
2017-8-4 11:35:09
Strings
A string is a sequence of values that represent Unicode code points. All the code points in the range U+0000 - U+10FFFF can be represented in a string. Python doesn’t have a char type; instead, every code point in the string is represented as a string object with length 1. The built-in function ord() converts a code point from its string form to an integer in the range 0 - 10FFFF; chr() converts an integer in the range 0 - 10FFFF to the corresponding length 1 string object. str.encode() can be used to convert a str to bytes using the given text encoding, and bytes.decode() can be used to achieve the opposite.

字符串
字符串是由表示Unicode码点的值组成的一个序列。在U+0000 - U+10FFFF范围内的所有码点都可以在字符串中表示。Python没有char类型;字符串中个每个码点通过长度为1的字符串对象表示。内建函数ord()将一个码点从字符串形式转换为范围在0 - 10FFFF之间的一个整数;chr()将0 - 10FFFF范围之间的一个整数转换为对应的长度为1的字符串对象。str.encode()可以用来使用给定的文本编码将str转换为bytes,bytes.decode()可以用来实现相反的操作。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-8-4 02:37:21 | 显示全部楼层
国际惯例 utf-8

  1. Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
  2. Type "copyright", "credits" or "license()" for more information.
  3. >>> import sys
  4. >>> sys.getdefaultencoding()
  5. 'utf-8'
  6. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-8-4 06:44:45 | 显示全部楼层

起的这么早
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-4 08:13:42 | 显示全部楼层

这个点他还没睡。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-4 08:16:49 | 显示全部楼层
冬雪雪冬 发表于 2017-8-4 08:13
这个点他还没睡。

每天都熬夜那么晚!?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-4 08:19:05 | 显示全部楼层
新手·ing 发表于 2017-8-4 08:16
每天都熬夜那么晚!?

大神都是要熬夜的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-4 08:19:51 | 显示全部楼层
冬雪雪冬 发表于 2017-8-4 08:19
大神都是要熬夜的。

对身体不好......
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-4 11:35:09 | 显示全部楼层    本楼为最佳答案   
Strings
A string is a sequence of values that represent Unicode code points. All the code points in the range U+0000 - U+10FFFF can be represented in a string. Python doesn’t have a char type; instead, every code point in the string is represented as a string object with length 1. The built-in function ord() converts a code point from its string form to an integer in the range 0 - 10FFFF; chr() converts an integer in the range 0 - 10FFFF to the corresponding length 1 string object. str.encode() can be used to convert a str to bytes using the given text encoding, and bytes.decode() can be used to achieve the opposite.

字符串
字符串是由表示Unicode码点的值组成的一个序列。在U+0000 - U+10FFFF范围内的所有码点都可以在字符串中表示。Python没有char类型;字符串中个每个码点通过长度为1的字符串对象表示。内建函数ord()将一个码点从字符串形式转换为范围在0 - 10FFFF之间的一个整数;chr()将0 - 10FFFF范围之间的一个整数转换为对应的长度为1的字符串对象。str.encode()可以用来使用给定的文本编码将str转换为bytes,bytes.decode()可以用来实现相反的操作。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-4 11:39:44 | 显示全部楼层


不是 UTF-8m 吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-4 12:16:24 | 显示全部楼层
  1. >>> import sys;sys.getdefaultencoding()
  2. 'utf-8'
  3. >>> s='字符串默认编码'
  4. >>> bui=bytes(s,'unicode_internal')
  5. >>> bui
  6. b'W[&{2N\xd8\x9e\xa4\x8b\x16\x7f\x01x'
  7. >>> u16le=bytes(s,'utf_16_le')
  8. >>> u16le
  9. b'W[&{2N\xd8\x9e\xa4\x8b\x16\x7f\x01x'
  10. >>> bui == u16le
  11. True
  12. >>> s[0]
  13. '字'
  14. >>> bui[0],bui[1]
  15. (87, 91)
  16. >>> chr(bui[1]*256 + bui[0])
  17. '字'
  18. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-4 12:21:54 | 显示全部楼层

getdefaultencoding 不是字符串编码,而是python 读写 .py源文件 的默认编码

python3的字符串编码,是在内存中用 unicode16-le 格式存储。

看 10楼 的例子。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 10:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表