Skip to Content
流畅的Python(第2版)
book

流畅的Python(第2版)

by Luciano Ramalho
April 2023
Intermediate to advanced
769 pages
25h 16m
Chinese
Posts & Telecom Press
Content preview from 流畅的Python(第2版)
108
4
情况,但是
Python
核心团队尽心尽力,提供了一种能满足多数用户需求的方案。
接下来的内容使用这些规范化知识开发几个实用函数。
4.7.2
 规范化文本匹配的实用函数
由前文可知,我们可以放心使用
NFC
NFD
比较
Unicode
字符串,结果是合理的。对多数
应用程序来说,
NFC
是最好的规范化形式。不区分大小写的比较应该使用
str.casefold()
如果需要处理多语言文本,你的工具箱应该增加示例
4-13
中的
nfc_equal
fold_equal
函数。
示例
4-13
normeq.py
:规范化
Unicode
字符串,准确比较
"""
规范化Unicode字符串的实用函数,确保准确比较。
使用NFC规范化形式,区分大小写:
>>> s1 = 'café'
>>> s2 = 'cafe\u0301'
>>> s1 == s2
False
>>> nfc_equal(s1, s2)
True
>>> nfc_equal('A', 'a')
False
使用NFC规范化形式,大小写同一化:
>>> s3 = 'Straße'
>>> s4 = 'strasse'
>>> s3 == s4
False
>>> nfc_equal(s3, s4)
False
>>> fold_equal(s3, s4)
True
>>> fold_equal(s1, s2)
True
>>> fold_equal('A', 'a')
True
""" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python高级编程(第2版)

Python高级编程(第2版)

Posts & Telecom Press, Michał Jaworski, Tarek Ziadé
Kafka权威指南(第2版)

Kafka权威指南(第2版)

Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty
Python贝叶斯分析(第2版)

Python贝叶斯分析(第2版)

Posts & Telecom Press, Osvaldo Martin

Publisher Resources

ISBN: 9787115612366