网站首页 > 技术文章 正文

Unicode 万国码
在 Python 3 中,字符串由 Unicode 表示,而不是字节。ASCII 码是定义字符数字代码的最著名的标准。数字值最初只定义 128 个字符,因此 ASCII 只包含控制代码、数字、小写字母、大写字母等。然而,我们不足以表示世界各地存在的重音字符、汉字或表情符号等字符。因此,Unicode 被开发来解决这个问题。它定义了代码点来表示各种字符,如 ASCII,但字符数多达 1,111,998 个。
String 字符串
在 Python 2 中,字符串以字节表示,而不是 Unicode。Python 提供了不同类型的字符串,例如 Unicode 字符串、原始字符串等。在这种情况下,如果我们想声明一个 Unicode 字符串,我们为字符串文字添加 u 前缀。
s = 'Café' # byte string
s
'Caf\xc3\xa9'
type(s)
<type 'str'>
u = u'Café' # unicode string
u
u'Caf\xe9'
type(u)
<type 'unicode'>
在 Python 3 中,字符串用 Unicode 表示。如果我们想表示字节字符串,我们为字符串文字添加 b 前缀。请注意,早期的 Python 版本(3.0-3.2)不支持 u 前缀。为了减轻从 Python 2 迁移 Unicode 感知应用程序的痛苦,Python 3.3 再次支持字符串文字的 u 前缀。
s = 'Café'
type(s)
<class 'str'>
s
'Café'
s.encode('utf-8')
b'Caf\xc3\xa9'
s.encode('utf-8').decode('utf-8')
'Café'
Characters 字符
Python 2 将所有字符串字符作为字节。在这种情况下,字符串的长度可能不等于字符的数量。例如,Café 的长度是 5,而不是 4,因为 é 被编码为 2 字节的字符。
s= 'Café'
print([_c for _c in s])
['C', 'a', 'f', '\xc3', '\xa9']
len(s)
5
s = u'Café'
print([_c for _c in s])
[u'C', u'a', u'f', u'\xe9']
len(s)
4
Python 3 将所有字符串字符视为 Unicode 码点。字符串的长度始终等同于字符的数量。
s = 'Café'
print([_c for _c in s])
['C', 'a', 'f', 'é']
len(s)
4
bs = bytes(s, encoding='utf-8')
print(bs)
b'Caf\xc3\xa9'
len(bs)
5
移植 unicode(s, ‘utf-8’)
unicode()内置函数在 Python 3 中被删除,那么转换表达式 unicode(s,'utf-8')的最佳兼容方法是什么?可以在Python2和Python3中通用呢?
# python2 版本
s = 'Café'
unicode(s, 'utf-8')
u'Caf\xe9'
s.decode('utf-8')
u'Caf\xe9'
unicode(s, 'utf-8') == s.decode('utf-8')
True
# python3 版本
s = 'Café'
s.decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'
以上栗子可以看到decode()不能对Unicode编码的字符串进行反编码。
ord函数
ord 是一个强大的内置函数,它用于返回给定字符的Unicode数值,即该字符在Unicode表中的位置。
s = u'Café'
for _c in s: print('U+%04x' % ord(_c))
U+0043
U+0061
U+0066
U+00e9
u = '中文'
for _c in u: print('U+%04x' % ord(_c))
U+4e2d
U+6587
Encoding 编码
Unicode 字符串转换为Byte字节即为encoding编码。
s = u'Café'
type(s.encode('utf-8'))
<class 'bytes'>
相反,如果Byte字节转为Unicode字符为decoding反编码。
s = bytes('Café', encoding='utf-8')
s.decode('utf-8')
'Café'
当Byte字节字符串无法解码为 Unicode 字符串时,Python 会引发 UnicodeDecodeError的异常。如果我们想避免这个异常,我们可以在解码中传递替换、反斜杠替换或忽略错误的参数。
u = b"\xff"
u.decode('utf-8', 'strict')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
# use U+FFFD, REPLACEMENT CHARACTER
u.decode('utf-8', "replace")
'\ufffd'
# inserts a \xNN escape sequence
u.decode('utf-8', "backslashreplace")
'\\xff'
# leave the character out of the Unicode result
u.decode('utf-8', "ignore")
''
定义长字符串
以下是定义多行长字符串的几种方法。
# 普通单引号
s = 'This is a very very very long python string'
# 反斜杠末尾断行
s = "This is a very very very " \
"long python string"
# 使用小括号
s = (
"This is a very very very "
"long python string"
)
# 使用+号连接
s = (
"This is a very very very " +
"long python string"
)
# 使用三引号加反斜杠
s = '''This is a very very very \
long python string'''
学废了嘛,如有错误,请指正,感谢。
猜你喜欢
- 2024-12-13 Python数据类型字符串的几种表示形式
- 2024-12-13 python 基础语法详解(入门必读)
- 2024-12-13 轻松掌握!Python 基本语法与核心数据类型全解析
- 2024-12-13 Python注释方式有哪些
- 2024-12-13 Word 神器 python-docx
- 2024-12-13 万字干货,Python语法大合集,一篇文章带你入门
- 2024-12-13 Python之open()函数
- 2024-12-13 编程语言python:数据类型
- 2024-12-13 Python基础语法到高级概念
- 2024-12-13 Python字符串单引号('...')和双引号("...")的区别
- 06-24Python调用Docker API的使用方式(pycharm docker 调试)
- 06-24青少年Python编程系列28:Python中函数的递归调用
- 06-24python调用sqlite数据库案例(python 调用数据库)
- 06-24【Python机器学习系列】基于Flask来构建API调用机器学习模型服务
- 06-24通过pybind11来实现python调用C++接口(一)
- 06-24Python编程调用Deepseek API创建智能体
- 06-24python多装饰器针对函数、类、方法的调用顺序说明
- 06-24Python Qt GUI设计:Python调用UI文件的两种方法(基础篇—3)
- 271℃Python短文,Python中的嵌套条件语句(六)
- 269℃PythonNet:实现Python与.Net代码相互调用!
- 269℃python笔记:for循环嵌套。end=""的作用,图形打印
- 263℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 263℃Python实现字符串小写转大写并写入文件
- 122℃原来2025是完美的平方年,一起探索六种平方的算吧
- 103℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 97℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
-
- Python调用Docker API的使用方式(pycharm docker 调试)
- 青少年Python编程系列28:Python中函数的递归调用
- python调用sqlite数据库案例(python 调用数据库)
- 【Python机器学习系列】基于Flask来构建API调用机器学习模型服务
- 通过pybind11来实现python调用C++接口(一)
- Python编程调用Deepseek API创建智能体
- python多装饰器针对函数、类、方法的调用顺序说明
- Python Qt GUI设计:Python调用UI文件的两种方法(基础篇—3)
- Python | Django 外部脚本调用 models 数据库
- 自学Python第九天——操作列表(python操作步骤)
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)