Skip to Content
Python入门指南, 3rd Edition
book

Python入门指南, 3rd Edition

by Bill Lubanovic
September 2025
Intermediate to advanced
660 pages
7h 15m
Chinese
O'Reilly Media, Inc.
Content preview from Python入门指南, 3rd Edition

第 17 章 文本数据 文本数据

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

我为文本而生。这是我的工作。

伊恩-麦凯伦

第 4 章中,我们了解了 Python 字符串的基础知识。 现在是深入研究字符串和文本数据的时候了。

文本字符串:统一字符编码

Python 3 字符串 是 Unicode 字符序列,而不是字节数组。到目前为止,这是与 Python 2 相比最大的语言变化。

迄今为止,本书中的所有 文本示例都是普通的 ASCII。ASCII 定义于 20 世纪 60 年代,那时地球上还没有鲻鱼。 那时的计算机只有冰箱那么大,只是稍微聪明一些。

正如我在第 2 章中提到的,计算机存储的基本单位是字节,它的 8 个比特可以存储 256 个唯一值。 由于种种原因,ASCII 只使用了 7 个比特(128 个唯一值):26 个大写字母、26 个小写字母、10 个数字、一些标点符号、一些间隔字符和一些非打印控制代码。

遗憾的是,世界上的字母数量远远超过 ASCII 所提供的数量,你可以在餐厅吃热狗,但绝不能在咖啡馆喝琼瑶浆。1 为了把更多的字母和符号塞进八个比特中,人们做了很多尝试,有时你会看到这些尝试。

下面就是其中的几种:

  • Latin-1ISO 8859-1

  • Windows 第1252页代码

每个字符都使用了全部八个比特,但即使这样也不够, ,尤其是当你需要使用非欧洲语言时。Unicode是一个正在进行中的国际标准,用于定义世界上所有语言的字符,以及数学和其他领域的符号。 还有表情符号!

统一码为每个字符提供了唯一的编号,无论使用何种平台、何种程序、何种语言。

统一码联盟

Unicode 代码图页面上有当前定义的所有字符集的链接和图片。 最新版本(16.0)定义了超过 155,000 个字符,每个字符都有一个唯一的名称和标识号。 Python 可以处理所有这些字符。 字符位分为 17 个平面,每个平面包含 65,536 个码位。 第一个平面,即基本多语言平面,包含了世界上大多数语言的字符。 详情请参见维基百科中有关 Unicode平面字符的页面。

Python Unicode 字符串

如果您知道某个字符的 Unicode ID 或名称,就可以在 Python 字符串中使用它。下面是一些例子:

  • \u 后面的四个十六进制数字表示 Unicode 256 个基本多语言平面中的一个字符。2 前两个数字是平面编号(00FF ),后两个数字是字符在平面内的索引。平面00 是古老的 ASCII 码,该平面内的字符位置与 ASCII 码相同。

  • 对于较高平面内的字符,我们需要更多的位。Python 的转义序列是\U,然后是八个十六进制字符;最左边的需要是0

  • 对于所有字符、\N{name}让您用标准名称指定字符Unicode 字符名称索引页面列出了这些名称。

Pythonunicodedata 模块中的函数可以进行双向翻译:

lookup()

取一个不区分大小写的名称并返回一个 Unicode 字符

name()

获取一个 Unicode 字符并返回一个大写名称

在下面的示例中,我们将编写一个测试函数,获取一个 Python Unicode 字符,查找它的名称,并根据名称再次查找该字符。 这个字符应该与原来的字符相匹配。请注意,print() 语句使用了 f-strings 的一个很酷的调试属性;请参阅"F-strings": ...

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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

生成式人工智能设计模式

生成式人工智能设计模式

Valliappa Lakshmanan, Hannes Hapke

Publisher Resources

ISBN: 9798341668898