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

第 18 章 二进制数据 二进制数据

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

任何足够清晰的问题都可以用一个二进制数字来回答-0 或 1,是或否。

卡尔-萨根

文本数据可能很有挑战性,但二进制数据也可能很有趣。您需要了解一些概念 ,如字节(计算机处理器将数据分解成字节的方式)和整数的符号位。您可能需要深入研究二进制文件格式或网络数据包,以提取甚至更改数据。 本节将向您展示在 Python 中处理二进制数据的基础知识。您将在第 20 章中看到如何读写二进制文件

用结构体转换二进制数据

正如您所看到的,Python 有许多处理文本的工具。处理二进制数据的工具要少得多。 标准库包含struct 模块,它处理的数据类似于 C 和 C++ 中的结构体。使用struct ,您可以将二进制数据转换为 Python 数据结构,也可以将二进制数据转换为 Python 数据结构。

我们将编写一个小程序,从一些 PNG 数据中提取图像的宽度和高度。

我们将使用 O'Reilly 的徽标,即图 18-1 中所示的小虫子眼睛的 Tarsier。

ipy3 1801
图 18-1. O'Reilly tarsier

这张图片的 PNG 文件可以在维基百科上找到。 我在第 20 章才介绍如何读取文件,所以我下载了这个文件,写了一个小程序将其值打印为字节,并将前 30 个字节的值输入一个名为data 的 Pythonbytes 变量,用于下面的示例。(PNG 格式规范规定,宽度和高度存储在前 24 个字节中,所以我们现在不需要更多的字节)。

代码如下:

>>> import struct
>>> valid_png_header = b'\x89PNG\r\n\x1a\n'
>>> data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR' + \
...     b'\x00\x00\x00\x9a\x00\x00\x00\x8d\x08\x02\x00\x00\x00\xc0'
>>> if data[:8] == valid_png_header:
...     width, height = struct.unpack('>LL', data[16:24])
...     print('Valid PNG, width', width, 'height', height)
... else:
...     print('Not a valid PNG')
...
Valid PNG, width 154 height 141

下面是这段代码的作用:

  • data 包含 PNG 文件的前 30 个字节。为使其适合在页面上显示,我用+和延续字符 (\) 连接了两个字节串。

  • valid_png_header 包含标志着有效 PNG 文件开始的 8 个字节序列。

  • width 从 16-19 字节中提取,height 从 20-23 字节中提取。

>LL 是格式字符串,它指示unpack() 如何解释输入的字节序列并将其组合成 Python 数据类型。下面是详细说明:

  • > 表示整数以大端格式存储。

  • 每个L 指定一个四字节无符号长整数。

您可以直接检查每个四字节值:

>>> 
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