二进制 I/O
¶
二进制 I/O (也称
缓冲 I/O
) 期望
像字节对象
并产生
bytes
对象。不履行编码、解码或翻译换行符。这种类别的流可以用于所有种类的非文本数据,且当期望手动控制文本数据的处理时也如此。
创建二进制流的最简单方式是采用
open()
with
'b'
在模式字符串下:
f = open("myfile.jpg", "rb")
内存二进制流还可用作
BytesIO
对象:
f = io.BytesIO(b"some initial binary data: \x00\x01")
二进制流 API 的详细描述在文档化的
BufferedIOBase
.
其它库模块可以提供创建文本 (或二进制) 流的额外方式。见
socket.socket.makefile()
例如。
文本编码
¶
默认编码对于
TextIOWrapper
and
open()
是特定区域设置 (
locale.getencoding()
).
However, many developers forget to specify the encoding when opening text files encoded in UTF-8 (e.g. JSON, TOML, Markdown, etc…) since most Unix platforms use UTF-8 locale by default. This causes bugs because the locale encoding is not UTF-8 for most Windows users. For example:
# May not work on Windows when non-ASCII characters in the file.
with open("README.md") as f:
long_description = f.read()
Accordingly, it is highly recommended that you specify the encoding explicitly when opening text files. If you want to use UTF-8, pass
encoding="utf-8"
. To use the current locale encoding,
encoding="locale"
is supported since Python 3.10.
选择加入 EncodingWarning
¶
Added in version 3.10:
见
PEP 597
了解更多细节。
To find where the default locale encoding is used, you can enable the
-X warn_default_encoding
command line option or set the
PYTHONWARNDEFAULTENCODING
environment variable, which will emit an
EncodingWarning
when the default encoding is used.
If you are providing an API that uses
open()
or
TextIOWrapper
and passes
encoding=None
as a parameter, you can use
text_encoding()
so that callers of the API will emit an
EncodingWarning
if they don’t pass an
encoding
. However, please consider using UTF-8 by default (i.e.
encoding="utf-8"
) for new APIs.
类层次结构
¶
I/O 流实现的组织是按类层次结构。首先
抽象基类
(ABC) 用于指定流的各种类别,然后具体类提供标准流实现。
注意
ABC (抽象基类) 还提供了一些方法的默认实现,以帮助实现具体的流类。例如,
BufferedIOBase
提供实现未优化的
readinto()
and
readline()
.
At the top of the I/O hierarchy is the abstract base class
IOBase
. It defines the basic interface to a stream. Note, however, that there is no separation between reading and writing to streams; implementations are allowed to raise
UnsupportedOperation
若它们不支持给定操作。
The
RawIOBase
ABC (抽象基类) 延伸
IOBase
. It deals with the reading and writing of bytes to a stream.
FileIO
子类
RawIOBase
to provide an interface to files in the machine’s file system.
The
BufferedIOBase
ABC (抽象基类) 延伸
IOBase
. It deals with buffering on a raw binary stream (
RawIOBase
)。其子类,
BufferedWriter
,
BufferedReader
,和
BufferedRWPair
buffer raw binary streams that are writable, readable, and both readable and writable, respectively.
BufferedRandom
provides a buffered interface to seekable streams. Another
BufferedIOBase
子类,
BytesIO
, is a stream of in-memory bytes.
The
TextIOBase
ABC (抽象基类) 延伸
IOBase
. It deals with streams whose bytes represent text, and handles encoding and decoding to and from strings.
TextIOWrapper
, which extends
TextIOBase
, is a buffered text interface to a buffered raw stream (
BufferedIOBase
). Finally,
StringIO
是用于文本的内存流。
Argument names are not part of the specification, and only the arguments of
open()
are intended to be used as keyword arguments.
下表汇总的 ABC (抽象基类) 提供通过
io
模块:
|
ABC (抽象基类)
|
继承
|
Stub 方法
|
混合方法和特性
|
|
IOBase
|
|
fileno
,
seek
,和
truncate
|
close
,
closed
,
__enter__
,
__exit__
,
flush
,
isatty
,
__iter__
,
__next__
,
readable
,
readline
,
readlines
,
seekable
,
tell
,
writable
,和
writelines
|
|
RawIOBase
|
IOBase
|
readinto
and
write
|
继承
IOBase
方法,
read
,和
readall
|
|
BufferedIOBase
|
IOBase
|
detach
,
read
,
read1
,和
write
|
继承
IOBase
方法,
readinto
,和
readinto1
|
|
TextIOBase
|
IOBase
|
detach
,
read
,
readline
,和
write
|
继承
IOBase
方法,
encoding
,
errors
,和
newlines
|
I/O 基类
¶
-
class
io.
IOBase
¶
-
所有 I/O 类的 ABC (抽象基类)。
此类为派生类可以选择性覆盖的很多方法提供空抽象实现;默认实现表示无法读取、写入或寻址的文件。
即使
IOBase
不声明
read()
or
write()
因为它们的签名有所不同,所以实现和客户端应将这些方法考虑成接口的一部分。此外,实现可能引发
ValueError
(或
UnsupportedOperation
) 当调用它们不支持的操作时。
用于从文件读取 (或写入) 二进制数据的基本类型是
bytes
。其它
像字节对象
还被接受作为方法自变量。文本 I/O 类工作采用
str
数据。
注意,调用关闭流的任何方法 (甚至质问) 都是未定义的。实现可能引发
ValueError
在此情况下。
IOBase
(及其子类) 支持迭代器协议,意味着
IOBase
对象可以迭代流中产生的行。行的定义有点差异从属流是二进制流 (产生字节) 或文本流 (产生字符串)。见
readline()
下文。
IOBase
也是上下文管理器,因此支持
with
语句。在此范例中,
file
被关闭后于
with
语句套件的完成 — 即使出现异常:
with open('spam.txt', 'w') as file:
file.write('Spam and eggs!')
IOBase
提供这些数据属性和方法:
-
close
(
)
¶
-
刷新并关闭该流。此方法不起作用,若文件已关闭。文件一旦被关闭,对文件的任何操作 (如:读取或写入) 都将引发
ValueError
.
为了方便,允许多次调用此方法;不管怎样,仅第一次调用有效。
-
closed
¶
-
True
若流被关闭。
-
fileno
(
)
¶
-
返回流的底层文件描述符 (整数) 若存在。
OSError
被引发若 IO 对象不使用文件描述符。
-
flush
(
)
¶
-
刷新流的写入缓冲,若适用。这什么都不做,对于只读和非阻塞流。
-
isatty
(
)
¶
-
返回
True
若流可交互 (即:连接到终端/tty 设备)。
-
readable
(
)
¶
-
返回
True
若流可以被读取。若
False
,
read()
会引发
OSError
.
-
readline
(
size
=
-1
,
/
)
¶
-
从流读取并返回一行。若
size
有指定,最多
size
字节将被读取。
行终止符始终是
b'\n'
对于二进制文件;对于文本文件,
newline
自变量对于
open()
可以用于选择识别行终止符。
-
readlines
(
hint
=
-1
,
/
)
¶
-
从流读取并返回行列表。
hint
可以指定要控制的读取行数:没有更多行将被读取,若到目前为止的所有行总大小 (以字节/字符为单位) 超过
hint
.
hint
值为
0
或小于,及
None
,视为没有提示。
注意,迭代文件对象已经是可能的使用
for
line in file: ...
不调用
file.readlines()
.
-
seek
(
offset
,
whence
=
os.SEEK_SET
,
/
)
¶
-
将流位置改为给定字节
offset
, interpreted relative to the position indicated by
whence
, and return the new absolute position. Values for
whence
是:
Added in version 3.1:
The
SEEK_*
常量。
Added in version 3.3:
某些操作系统可以支持额外值,像
os.SEEK_HOLE
or
os.SEEK_DATA
。文件有效值从属它是以文本模式打开,还是以二进制模式打开。
-
seekable
(
)
¶
-
返回
True
若流支持随机访问。若
False
,
seek()
,
tell()
and
truncate()
会引发
OSError
.
-
tell
(
)
¶
-
返回当前流的位置。
-
truncate
(
size
=
None
,
/
)
¶
-
重置流大小到给定
size
以字节为单位 (或当前位置若
size
不指定)。当前流位置不改变。这种重置大小可以扩展 (或缩减) 当前文件的大小。在扩展情况下,新文件区域的内容从属平台 (在大多数系统,额外字节以 0 填充)。返回新的文件大小。
3.5 版改变:
Windows 现在以 0 填充文件,当扩展时。
-
writable
(
)
¶
-
返回
True
若流支持写入。若
False
,
write()
and
truncate()
会引发
OSError
.
-
writelines
(
lines
,
/
)
¶
-
Write a list of lines to the stream. Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.
-
__del__
(
)
¶
-
准备销毁对象。
IOBase
提供此方法的默认实现,调用实例的
close()
方法。
-
class
io.
RawIOBase
¶
-
Base class for raw binary streams. It inherits from
IOBase
.
原生二进制流通常提供对底层 OS 设备或 API 的低级访问,且不会试着将它封装在高级原语中 (这种功能在更高级缓冲二进制流和文本流中完成,本页面稍后会描述)。
RawIOBase
提供这些方法,除了那些来自
IOBase
:
-
read
(
size
=
-1
,
/
)
¶
-
读取直到
size
字节从对象并返回它们。为了方便,若
size
未指定或为 -1,返回所有字节直到 EOF (文件末尾)。否则,只做一次系统调用。少于
size
字节也可能返回若操作系统调用返回小于
size
字节。
若返回 0 字节,和
size
非 0,这指示 EOF (文件末尾)。若对象处于非阻塞模式且没有可用字节,
None
被返回。
遵从默认实现对于
readall()
and
readinto()
.
-
readall
(
)
¶
-
读取并返回来自流的所有字节直到 EOF (文件末尾),使用多次流调用若有必要。
-
readinto
(
b
,
/
)
¶
-
将字节读入预分配,可写
像字节对象
b
, and return the number of bytes read. For example,
b
可以是
bytearray
. If the object is in non-blocking mode and no bytes are available,
None
被返回。
-
write
(
b
,
/
)
¶
-
写入给定
像字节对象
,
b
, to the underlying raw stream, and return the number of bytes written. This can be less than the length of
b
in bytes, depending on specifics of the underlying raw stream, and especially if it is in non-blocking mode.
None
is returned if the raw stream is set not to block and no single byte could be readily written to it. The caller may release or mutate
b
after this method returns, so the implementation should only access
b
在方法调用期间。
-
class
io.
BufferedIOBase
¶
-
Base class for binary streams that support some kind of buffering. It inherits from
IOBase
.
主要差异相比
RawIOBase
是方法
read()
,
readinto()
and
write()
将 (分别) 试着按请求读取尽可能多的输入 (或消耗所有给定输出),以做出或许不止一次的系统调用为代价。
此外,这些方法会引发
BlockingIOError
若底层原生流处于非阻塞模式下且无法获得 (或给出) 足够数据;不像它们的
RawIOBase
搭档,他们从不会返回
None
.
此外,
read()
方法没有遵从默认实现对于
readinto()
.
典型
BufferedIOBase
实现不应继承自
RawIOBase
实现,但包裹某个,像
BufferedWriter
and
BufferedReader
做的。
BufferedIOBase
提供 (或覆写) 了这些数据属性和方法,除了那些来自
IOBase
:
-
raw
¶
-
底层原生流 (
RawIOBase
实例)
BufferedIOBase
的处理。这不属于
BufferedIOBase
API 且在某些实现中可能不存在。
-
detach
(
)
¶
-
从缓冲分离底层原生流并返回它。
在原生流被分离后,缓冲处于不可用状态。
某些缓冲,像
BytesIO
, do not have the concept of a single raw stream to return from this method. They raise
UnsupportedOperation
.
Added in version 3.1.
-
read
(
size
=
-1
,
/
)
¶
-
读取并返回直到
size
bytes. If the argument is omitted,
None
, or negative, data is read and returned until EOF is reached. An empty
bytes
object is returned if the stream is already at EOF.
If the argument is positive, and the underlying raw stream is not interactive, multiple raw reads may be issued to satisfy the byte count (unless EOF is reached first). But for interactive raw streams, at most one raw read will be issued, and a short result does not imply that EOF is imminent.
A
BlockingIOError
is raised if the underlying raw stream is in non blocking-mode, and has no data available at the moment.
-
read1
(
size
=
-1
,
/
)
¶
-
读取并返回直到
size
bytes, with at most one call to the underlying raw stream’s
read()
(或
readinto()
) method. This can be useful if you are implementing your own buffering on top of a
BufferedIOBase
对象。
若
size
is
-1
(the default), an arbitrary number of bytes are returned (more than zero unless EOF is reached).
-
readinto
(
b
,
/
)
¶
-
将字节读入预分配,可写
像字节对象
b
and return the number of bytes read. For example,
b
可以是
bytearray
.
像
read()
, multiple reads may be issued to the underlying raw stream, unless the latter is interactive.
A
BlockingIOError
is raised if the underlying raw stream is in non blocking-mode, and has no data available at the moment.
-
readinto1
(
b
,
/
)
¶
-
将字节读入预分配,可写
像字节对象
b
, using at most one call to the underlying raw stream’s
read()
(或
readinto()
) method. Return the number of bytes read.
A
BlockingIOError
is raised if the underlying raw stream is in non blocking-mode, and has no data available at the moment.
Added in version 3.5.
-
write
(
b
,
/
)
¶
-
写入给定
像字节对象
,
b
, and return the number of bytes written (always equal to the length of
b
in bytes, since if the write fails an
OSError
will be raised). Depending on the actual implementation, these bytes may be readily written to the underlying stream, or held in a buffer for performance and latency reasons.
当在非阻塞模式下时,
BlockingIOError
is raised if the data needed to be written to the raw stream but it couldn’t accept all the data without blocking.
The caller may release or mutate
b
after this method returns, so the implementation should only access
b
在方法调用期间。
原生文件 I/O
¶
-
class
io.
FileIO
(
名称
,
mode
=
'r'
,
closefd
=
True
,
opener
=
None
)
¶
-
A raw binary stream representing an OS-level file containing bytes data. It inherits from
RawIOBase
.
The
name
可以是 2 件事之一:
-
字符串或
bytes
object representing the path to the file which will be opened. In this case closefd must be
True
(默认),否则,会引发错误。
-
an integer representing the number of an existing OS-level file descriptor to which the resulting
FileIO
object will give access. When the FileIO object is closed this fd will be closed as well, unless
closefd
被设为
False
.
The
mode
可以是
'r'
,
'w'
,
'x'
or
'a'
for reading (default), writing, exclusive creation or appending. The file will be created if it doesn’t exist when opened for writing or appending; it will be truncated when opened for writing.
FileExistsError
will be raised if it already exists when opened for creating. Opening a file for creating implies writing, so this mode behaves in a similar way to
'w'
。添加
'+'
to the mode to allow simultaneous reading and writing.
The
read()
(when called with a positive argument),
readinto()
and
write()
methods on this class will only make one system call.
自定义开启器可以用于传递可调用如
opener
。然后,获得文件对象底层文件描述符通过调用
opener
采用 (
name
,
flags
).
opener
必须返回打开文件描述符 (传递
os.open
as
opener
导致功能类似于传递
None
).
新近创建的文件
不可继承
.
见
open()
内置函数,例如使用
opener
参数。
3.3 版改变:
The
opener
参数被添加。
'x'
模式被添加。
3.4 版改变:
文件现在不可继承。
FileIO
提供这些数据属性,除了那些来自
RawIOBase
and
IOBase
:
-
mode
¶
-
在构造函数中给定的模式。
-
名称
¶
文件名。这是文件的文件描述符当构造函数中未给定名称时。
缓冲流
¶
缓冲 I/O 流为 I/O 设备提供更高级接口,相比原生 I/O。
-
class
io.
BytesIO
(
initial_bytes
=
b''
)
¶
-
A binary stream using an in-memory bytes buffer. It inherits from
BufferedIOBase
。丢弃缓冲当
close()
方法被调用。
可选自变量
initial_bytes
是
像字节对象
包含初始数据。
BytesIO
提供或覆盖这些方法,除了那些来自
BufferedIOBase
and
IOBase
:
-
getbuffer
(
)
¶
-
返回可读/可写视图,在不拷贝缓冲内容的情况下。还有,变异视图会透明地更新缓冲内容:
>>> b = io.BytesIO(b"abcdef")
>>> view = b.getbuffer()
>>> view[2:4] = b"56"
>>> b.getvalue()
b'ab56ef'
注意
只要视图存在,
BytesIO
对象不可以重置大小或关闭。
Added in version 3.2.
-
getvalue
(
)
¶
返回
bytes
包含缓冲的整个内容。
-
read1
(
size
=
-1
,
/
)
¶
-
在
BytesIO
,这如同
read()
.
3.7 版改变:
The
size
自变量现在是可选的。
-
readinto1
(
b
,
/
)
¶
-
在
BytesIO
,这如同
readinto()
.
Added in version 3.5.
-
class
io.
BufferedReader
(
raw
,
buffer_size
=
DEFAULT_BUFFER_SIZE
)
¶
-
缓冲二进制流提供更高级访问对可读、不可寻址
RawIOBase
raw binary stream. It inherits from
BufferedIOBase
.
当从此对象读取数据时,从底层原生流请求的数据量可能更大,并保持在内部缓冲。然后,后续读取时可以直接返回缓冲数据。
构造函数创建
BufferedReader
对于给定可读
raw
流和
buffer_size
。若
buffer_size
被省略,
DEFAULT_BUFFER_SIZE
被使用。
BufferedReader
提供或覆盖这些方法,除了那些来自
BufferedIOBase
and
IOBase
:
-
peek
(
size
=
0
,
/
)
¶
-
Return bytes from the stream without advancing the position. At most one single read on the raw stream is done to satisfy the call. The number of bytes returned may be less or more than requested.
-
read
(
size
=
-1
,
/
)
¶
-
读取并返回
size
bytes, or if
size
is not given or negative, until EOF or if the read call would block in non-blocking mode.
-
read1
(
size
=
-1
,
/
)
¶
-
读取并返回直到
size
bytes with only one call on the raw stream. If at least one byte is buffered, only buffered bytes are returned. Otherwise, one raw stream read call is made.
3.7 版改变:
The
size
自变量现在是可选的。
-
class
io.
BufferedWriter
(
raw
,
buffer_size
=
DEFAULT_BUFFER_SIZE
)
¶
-
A buffered binary stream providing higher-level access to a writeable, non seekable
RawIOBase
raw binary stream. It inherits from
BufferedIOBase
.
When writing to this object, data is normally placed into an internal buffer. The buffer will be written out to the underlying
RawIOBase
object under various conditions, including:
构造函数创建
BufferedWriter
for the given writeable
raw
stream. If the
buffer_size
不给定,默认为
DEFAULT_BUFFER_SIZE
.
BufferedWriter
提供或覆盖这些方法,除了那些来自
BufferedIOBase
and
IOBase
:
-
flush
(
)
¶
-
Force bytes held in the buffer into the raw stream. A
BlockingIOError
should be raised if the raw stream blocks.
-
write
(
b
,
/
)
¶
-
写入
像字节对象
,
b
,并返回写入字节数。当在非阻塞模式下,
BlockingIOError
被引发若需要写出缓冲,但原生流阻塞。
-
class
io.
BufferedRandom
(
raw
,
buffer_size
=
DEFAULT_BUFFER_SIZE
)
¶
-
A buffered binary stream providing higher-level access to a seekable
RawIOBase
raw binary stream. It inherits from
BufferedReader
and
BufferedWriter
.
The constructor creates a reader and writer for a seekable raw stream, given in the first argument. If the
buffer_size
is omitted it defaults to
DEFAULT_BUFFER_SIZE
.
BufferedRandom
is capable of anything
BufferedReader
or
BufferedWriter
can do. In addition,
seek()
and
tell()
are guaranteed to be implemented.
-
class
io.
BufferedRWPair
(
reader
,
writer
,
buffer_size
=
DEFAULT_BUFFER_SIZE
,
/
)
¶
-
A buffered binary stream providing higher-level access to two non seekable
RawIOBase
raw binary streams—one readable, the other writeable. It inherits from
BufferedIOBase
.
reader
and
writer
are
RawIOBase
objects that are readable and writeable respectively. If the
buffer_size
is omitted it defaults to
DEFAULT_BUFFER_SIZE
.
BufferedRWPair
实现所有的
BufferedIOBase
方法除了
detach()
,其引发
UnsupportedOperation
.
警告
BufferedRWPair
does not attempt to synchronize accesses to its underlying raw streams. You should not pass it the same object as reader and writer; use
BufferedRandom
代替。
文本 I/O
¶
-
class
io.
TextIOBase
¶
-
Base class for text streams. This class provides a character and line based interface to stream I/O. It inherits from
IOBase
.
TextIOBase
提供 (或覆写) 了这些数据属性和方法,除了那些来自
IOBase
:
-
encoding
¶
-
用于将流字节解码成字符串,和将字符串编码成字节的编码名称。
-
errors
¶
-
解码器 (或编码器) 的错误设置。
-
newlines
¶
-
字符串、字符串元组、或
None
,指示到目前为止翻译的换行符。从属实现和初始构造函数标志,这可能不可用。
-
buffer
¶
-
底层二进制缓冲 (
BufferedIOBase
实例)
TextIOBase
的处理。这不属于
TextIOBase
API 且在某些实现中可能不存在。
-
detach
(
)
¶
-
分隔底层二进制缓冲从
TextIOBase
并返回它。
分离底层缓冲后,
TextIOBase
处于不可用状态。
某些
TextIOBase
实现,像
StringIO
,可能没有底层缓冲概念且调用此方法会引发
UnsupportedOperation
.
Added in version 3.1.
-
read
(
size
=
-1
,
/
)
¶
-
读取并返回最多
size
字符从流作为单
str
。若
size
为负或者
None
,读取直到 EOF (文件末尾)。
-
readline
(
size
=
-1
,
/
)
¶
-
读取直到换行符或 EOF (文件末尾) 并返回单
str
。若流已在 EOF (文件末尾),返回空字符串。
若
size
有指定,最多
size
字符将被读取。
-
seek
(
offset
,
whence
=
SEEK_SET
,
/
)
¶
-
Change the stream position to the given
offset
. Behaviour depends on the
whence
parameter. The default value for
whence
is
SEEK_SET
.
-
SEEK_SET
or
0
: seek from the start of the stream (the default);
offset
must either be a number returned by
TextIOBase.tell()
, or zero. Any other
offset
value produces undefined behaviour.
-
SEEK_CUR
or
1
: “seek” to the current position;
offset
must be zero, which is a no-operation (all other values are unsupported).
-
SEEK_END
or
2
: seek to the end of the stream;
offset
must be zero (all other values are unsupported).
以不透明数字形式返回新的绝对位置。
Added in version 3.1:
The
SEEK_*
常量。
-
tell
(
)
¶
-
Return the current stream position as an opaque number. The number does not usually represent a number of bytes in the underlying binary storage.
-
write
(
s
,
/
)
¶
-
写入字符串
s
到流并返回写入字符数。
-
class
io.
TextIOWrapper
(
buffer
,
encoding
=
None
,
errors
=
None
,
newline
=
None
,
line_buffering
=
False
,
write_through
=
False
)
¶
-
缓冲文本流提供更高级访问对
BufferedIOBase
buffered binary stream. It inherits from
TextIOBase
.
encoding
gives the name of the encoding that the stream will be decoded or encoded with. It defaults to
locale.getencoding()
.
encoding="locale"
can be used to specify the current locale’s encoding explicitly. See
文本编码
了解更多信息。
errors
is an optional string that specifies how encoding and decoding errors are to be handled. Pass
'strict'
会引发
ValueError
exception if there is an encoding error (the default of
None
has the same effect), or pass
'ignore'
to ignore errors. (Note that ignoring encoding errors can lead to data loss.)
'replace'
导致置换标记 (譬如
'?'
) 被插入在畸形数据处。
'backslashreplace'
causes malformed data to be replaced by a backslashed escape sequence. When writing,
'xmlcharrefreplace'
(replace with the appropriate XML character reference) or
'namereplace'
(replace with
\N{...}
escape sequences) can be used. Any other error handling name that has been registered with
codecs.register_error()
is also valid.
newline
controls how line endings are handled. It can be
None
,
''
,
'\n'
,
'\r'
,和
'\r\n'
。其工作如下:
-
当从流读取输入时,若
newline
is
None
,
通用换行符
mode is enabled. Lines in the input can end in
'\n'
,
'\r'
,或
'\r\n'
,且这些会被翻译成
'\n'
before being returned to the caller. If
newline
is
''
, universal newlines mode is enabled, but line endings are returned to the caller untranslated. If
newline
has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
-
当写入输出到流时,若
newline
is
None
,任何
'\n'
写入字符被翻译成系统默认行分隔符,
os.linesep
。若
newline
is
''
or
'\n'
,不发生翻译。若
newline
是任何其它合法值,任何
'\n'
写入字符被翻译成给定字符串。
若
line_buffering
is
True
,
flush()
is implied when a call to write contains a newline character or a carriage return.
若
write_through
is
True
,调用
write()
are guaranteed not to be buffered: any data written on the
TextIOWrapper
object is immediately handled to its underlying binary
buffer
.
3.3 版改变:
The
write_through
自变量被添加。
3.3 版改变:
默认
encoding
现为
locale.getpreferredencoding(False)
而不是
locale.getpreferredencoding()
. Don’t change temporary the locale encoding using
locale.setlocale()
, use the current locale encoding instead of the user preferred encoding.
3.10 版改变:
The
encoding
自变量现在支持
"locale"
虚设编码名称。
TextIOWrapper
提供这些数据属性和方法,除了那些来自
TextIOBase
and
IOBase
:
-
line_buffering
¶
-
行缓冲是否被启用。
-
write_through
¶
-
Whether writes are passed immediately to the underlying binary buffer.
3.7 版添加。
-
reconfigure
(
*
,
encoding
=
None
,
errors
=
None
,
newline
=
None
,
line_buffering
=
None
,
write_through
=
None
)
¶
-
重新配置此文本流使用新设置为
encoding
,
errors
,
newline
,
line_buffering
and
write_through
.
未指定参数保持当前设置,除了
errors='strict'
的使用当
encoding
有指定但
errors
未指定。
改变 encoding 或 newline 不可能,若已从流读取了一些数据。另一方面,写入后改变 encoding 是可能的。
此方法做隐式流刷新,在设置新参数前。
3.7 版添加。
3.11 版改变:
方法支持
encoding="locale"
选项。
-
seek
(
cookie
,
whence
=
os.SEEK_SET
,
/
)
¶
-
Set the stream position. Return the new stream position as an
int
.
Four operations are supported, given by the following argument combinations:
-
seek(0, SEEK_SET)
: Rewind to the start of the stream.
-
seek(cookie, SEEK_SET)
: Restore a previous position;
cookie
必须为
a number returned by
tell()
.
-
seek(0, SEEK_END)
: Fast-forward to the end of the stream.
-
seek(0, SEEK_CUR)
: Leave the current stream position unchanged.
Any other argument combinations are invalid, and may raise exceptions.
-
tell
(
)
¶
-
Return the stream position as an opaque number. The return value of
tell()
can be given as input to
seek()
, to restore a previous stream position.
-
class
io.
StringIO
(
initial_value
=
''
,
newline
=
'\n'
)
¶
-
A text stream using an in-memory text buffer. It inherits from
TextIOBase
.
文本缓冲被丢弃当
close()
方法被调用。
The initial value of the buffer can be set by providing
initial_value
. If newline translation is enabled, newlines will be encoded as if by
write()
. The stream is positioned at the start of the buffer which emulates opening an existing file in a
w+
mode, making it ready for an immediate write from the beginning or for a write that would overwrite the initial value. To emulate opening a file in an
a+
mode ready for appending, use
f.seek(0, io.SEEK_END)
to reposition the stream at the end of the buffer.
The
newline
自变量的工作像
TextIOWrapper
, except that when writing output to the stream, if
newline
is
None
, newlines are written as
\n
在所有平台。
StringIO
提供此方法,除了那些来自
TextIOBase
and
IOBase
:
-
getvalue
(
)
¶
-
返回
str
containing the entire contents of the buffer. Newlines are decoded as if by
read()
, although the stream position is not changed.
用法范例:
import io
output = io.StringIO()
output.write('First line.\n')
print('Second line.', file=output)
# Retrieve file contents -- this will be
# 'First line.\nSecond line.\n'
contents = output.getvalue()
# Close object and discard memory buffer --
# .getvalue() will now raise an exception.
output.close()
-
class
io.
IncrementalNewlineDecoder
¶
-
解码换行符的帮手编解码器,对于
通用换行符
mode. It inherits from
codecs.IncrementalDecoder
.