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

第 23 章 盒中数据:持久存储

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

计算机能做的一件事,大多数人都做不了,那就是把它封在纸箱里,放在仓库里。

杰克-汉迪

运行中的程序会访问存储在随机访问内存或 RAM 中的数据。RAM 速度非常快,但价格昂贵,而且需要持续供电;如果断电,内存中的所有数据都会丢失。磁盘 驱动器的速度比 RAM 慢,但容量更大,成本更低,而且即使有人被电源线绊倒也能保留数据。 因此,计算机系统花费了大量精力在磁盘和 RAM 存储数据之间进行最佳权衡。作为 程序员,我们需要持久性:使用磁盘等非易失性介质存储和检索数据。

20 章将介绍输入和输出等文件操作。

文本文件

最简单的持久方法是使用普通的 老式平面文本文件。如果数据结构简单,而且可以在磁盘和内存之间交换所有数据,那么这种方法就很有效。 普通文本数据可能适合这种处理方法。

记录是相关数据块的术语,由 单个字段组成。

填充文本文件中,记录中的每个字段都有固定的宽度,数据在文件中被填充(通常是空格字符)到该宽度,使每行(记录)具有相同的宽度。程序员可以使用seek() 在文件中跳转,只读写需要的记录和字段。

表格和分隔文本文件

对于简单的文本文件,唯一的组织层次就是行。 有时,你需要比这更多的结构。 你可能想保存数据供你的程序以后使用,或者将数据发送到另一个程序。

文本文件可以有多种格式,以下是区分它们的方法:

  • 分隔符定界符,如制表符 (\t)、逗号 (,) 或竖条 (|)。这是逗号分隔值(CSV)格式的示例。

  • <> 标记。例如 XML 和 HTML。

  • 标点符号。例如 JavaScript Object Notation (JSON)。

  • 缩进。例如 YAML(递归定义为YAML Ain't Markup Language)。

  • 杂项,如程序的配置文件。

这些结构化文件格式中的每一种都能被至少一个 Python 模块读写。

CSV

分隔的 文件经常被用作电子表格和数据库的交换格式。你可以手动读取 CSV 文件,每次读取一行,用逗号分隔符将每行分割成字段,并将结果添加到列表和字典等数据结构中。但最好使用 标准csv 模块,因为解析这些文件可能比你想象的要复杂得多。以下是使用 CSV 时需要注意的几个重要特征:

  • 有些数据库除了逗号外还有其他定界符;常见的有|\t (tab)。

  • 有些数据库有转义序列,如果分隔符可以出现在一个字段中,那么整个字段可能会被引号字符包围,或者前面有一个转义字符。

  • 文件有不同的行结束字符。Unix 使用\n ,Microsoft 使用\r\n ,Apple 以前使用\r ,但现在使用\n

  • 第一行可能包含列名。

首先,我们将看到如何读写行列表,每一行都包含列列表:

>>> import csv
>>> villains = [
...     ['Doctor', 'No'],
...     ['Rosa', 'Klebb'],
...     ['Mister', 'Big'],
...     ['Auric', 'Goldfinger'],
...     ['Ernst', 'Blofeld'],
...     ]
>>> with open('villains', 'wt') as fout:
...     csvout = csv.writer(fout)
...     csvout.writerows(villains)

with open ...

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