python字符串编码解码基础知识

python字符串编码解码基础知识

python通过ord©获取字符c的unicode的编码值,为整数。通过chr(i)获取i对应的unicode的字符。通过str.encode()将字符串编码为原始字节,b.decode()将原始字节解码为字符串。

1 字符串基础知识

python通过ord©获取字符c的unicode的编码值,为整数。

通过chr(i)获取i对应的unicode的字符。

1.1 字符编码方法

ASCII码定义0-127的字符代码,每个字符存储在一个8位的字节中。

# 内置函数 描述
1 ord© 返回字符c的unicode的编码值,为整数
2 chr(i) 返回unicode编码为i的字符
3 help(encodings) 先import encodings,再help,查看python支持的编码名称

从字符串编码为原始字节,从原始字节解码字符串。

# 名称 描述
1 编码 根据编码名称,把字符串翻译为原始字节
2 解码 根据编码名称,把原始字节翻译为字符串

示例

python 复制代码
>>> ord('梯')
26799
>>> chr(26799)
'梯'

1.2 python字符串类型

# 版本 描述
1 py2.x str表示8位文本和二进制数据
2 unicode表示宽字符unicode文本
3 py3.x str表示unicode文本
4 bytes表示二进制数据
5 bytearray,可变的bytes类型

1.3 文本和二进制文件

# 模式 描述
1 bytes和二进制模式 处理图像文件、解压的打包数据、设备数据流
2 str和文本模式 用于程序输出、HTML、CSV、XML文件

2 python3.0的字符串应用

2.1 常量和基本属性

# 项目 描述
1 'xxx'和"xxx" 创建str,单字节或者更长的字节,存放字符串
2 '''xxx'''和"""xxx""" 创建str
3 'xxx'和"xxx"前面加b或B 创建bytes,单字节,存放字符对应原始字节的整数
4 '''xxx'''和"""xxx"""前加b或B 创建bytes

示例

python 复制代码
>>> b=b'abc' # 返回每个字符的原始字节的整数
>>> s='abc'
>>> type(b),type(s)
(<class 'bytes'>, <class 'str'>)
>>> b,s
(b'abc', 'abc')
>>> b[0],s[0]
(97, 'a')
>>> b[1:],s[1:]
(b'bc', 'bc')
>>> list(b),list(s)
([97, 98, 99], ['a', 'b', 'c'])
# bytes 和 str 不可修改
>>> b[0]='d'
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    b[0]='d'
TypeError: 'bytes' object does not support item assignment
>>> s[0]='d'
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    s[0]='d'
TypeError: 'str' object does not support item assignment
>>> c=b'''
abc
def
'''
>>> c
b'\nabc\ndef\n'

2.2 转换

# 项目 描述
1 str.encode()和 bytes(s,encoding) 编码,把字符串转换为bytes形式,根据str创建一个bytes。
2 bytes.decode()和 str(b,encoding) 解码,把bytes转换为字符串形式,根据bytes创建一个str。
3 sys.getdefaultencoding() 查看系统默认编码

描述

str.encode(encoding),根据编码名encoding将字符串str编码为原始字节,encoding默认为utf-8。

bytes(s,encoding),根据编码名encoding将字符串s编码为原始字节,encoding必填。

bytes.decode(encoding),根据编码名encoding将原始字节解码为字符串,encoding默认为utf-8。

str(b,encoding),根据编码名encoding将原始字节解码为字符串,encoding不填则打印原始字节。

示例

python 复制代码
>>> import sys
# 查看系统
>>> sys.platform
'win32'
# 查看系统默认编码
>>> sys.getdefaultencoding()
'utf-8'
>>> s='梯'
# encode 和 bytes 编码,字符串转原始字节
>>> s.encode()# encoding 默认为 utf-8
b'\xe6\xa2\xaf'
>>> s.encode(encoding='utf-8')
b'\xe6\xa2\xaf'
>>> s.encode(encoding='gbk')
b'\xcc\xdd'
>>> bytes(s,encoding='gbk')
b'\xcc\xdd'
>>> bytes(s,encoding='utf-8')
b'\xe6\xa2\xaf'
# decode 和 str 解码,原始字节转字符串
>>> b=b'\xe6\xa2\xaf'
>>> b.decode()
'梯'
>>> str(b,encoding='utf-8')
'梯'
# str() 没有 encoding ,则 进行打印
>>> str(b)
"b'\\xe6\\xa2\\xaf'"
# bytes() 没有 encoding ,则 报错
>>> bytes(s)
Traceback (most recent call last):
  File "<pyshell#62>", line 1, in <module>
    bytes(s)
TypeError: string argument without an encoding
相关推荐
用户70887690393838 分钟前
给传统 SaaS 增加 AI 能力:3个真实落地场景
python
卷无止境40 分钟前
FastAPI中间件全解析:请求处理链条上的隐形关卡
后端·python·fastapi
用户03321266636743 分钟前
使用 Python 将 HTML 内容添加到 PowerPoint 演示文稿中
python·html
看浪的路人44 分钟前
第4讲:MCP Client 开发——连接、发现、调用
windows·python·ai
Swift社区1 小时前
Python 开发环境怎么选?PyCharm、VS Code、Trae 谁更适合 AI 开发?
人工智能·python·pycharm
卷无止境1 小时前
聊聊Web开发里的流式数据 从原理到FastAPI实战
后端·python·fastapi
SMF19191 小时前
【PyCharm】让 PyCharm 使用 .venv 虚拟环境
ide·python·pycharm
修远客1 小时前
感知模块:Agent的眼睛和耳朵 — 三层降级策略让Agent永不"失明"
python·agent
circuitsosk1 小时前
Prompt Engineering进阶:面向复杂业务场景的模板化管理与动态注入策略
python·langchain·prompt·跨境电商·rag·上下文管理·动态注入
Livia要学习2 小时前
Python闭包
开发语言·jvm·python