Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
328
|
10 章 檔案與文字作業
記憶體內部的「檔案」:io.StringIO
io.BytesIO
你可以撰寫提供你所需方法的 Python 類別來實作類檔案物件(file-like
objects)。如果你想要的只是資料留存在記憶體中,而非在作業系統所見
的檔案上,就用
io
模組的類別
StringIO
BytesIO
。它們之間的差異在
於,
StringIO
的實體是文字模式的「檔案」,所以讀取或寫入會消耗或產生
Unicode 字串,而
BytesIO
的實體是二進位的「檔案」,所以讀取或寫入會
消耗或產生 bytestrings(位元組字串)。
實體化其中任一個類別時,你可以選擇性地傳入一個字串引數,分別是
Unicode bytes,來用作該「檔案」的初始內容。兩者中任一個類別的實
f
,除了「檔案」的方法,還提供一個額外的方法:
getvalue f.getvalue()
f
目前的資料內容回傳為一個字串(文字的或位元組的)。你無法在呼
f.close
之後呼叫
f.getvalue
close
會釋放
f
內部所維護的緩衝區,而
getvalue
需要回傳該緩衝區作為結果。
壓縮檔
儲存空間和傳輸頻寬越來越便宜與充沛,但在許多情況中,你可以藉由壓
縮(compression),花費一些額外的計算成本來節省這種資源。計算能力
變便宜且充裕的速度甚至比其他的某些資源(例如頻寬)還要快,所以壓
縮受歡迎的程度持續增長。Python 能讓你的程式輕易地支援壓縮,因為
Python 標準程式庫含有數個壓縮專用的模組。 ...
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

利用 Python 进行数据分析(原书第2版)

利用 Python 进行数据分析(原书第2版)

Wes McKinney

Publisher Resources

ISBN: 9789864766819