?? stream-reader-objects.html
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>4.7.1.3 StreamReader Objects </title>
<META NAME="description" CONTENT="4.7.1.3 StreamReader Objects ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="stream-reader-writer.html" tppabs="http://www.python.org/doc/current/lib/stream-reader-writer.html">
<LINK REL="previous" href="stream-writer-objects.html" tppabs="http://www.python.org/doc/current/lib/stream-writer-objects.html">
<LINK REL="up" HREF="node80.html" tppabs="http://www.python.org/doc/current/lib/node80.html">
<LINK REL="next" href="stream-reader-writer.html" tppabs="http://www.python.org/doc/current/lib/stream-reader-writer.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="stream-writer-objects.html" tppabs="http://www.python.org/doc/current/lib/stream-writer-objects.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="node80.html" tppabs="http://www.python.org/doc/current/lib/node80.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="stream-reader-writer.html" tppabs="http://www.python.org/doc/current/lib/stream-reader-writer.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="stream-writer-objects.html" tppabs="http://www.python.org/doc/current/lib/stream-writer-objects.html">4.7.1.2 StreamWriter Objects</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="node80.html" tppabs="http://www.python.org/doc/current/lib/node80.html">4.7.1 Codec Base Classes</A>
<b class="navlabel">Next:</b> <a class="sectref" href="stream-reader-writer.html" tppabs="http://www.python.org/doc/current/lib/stream-reader-writer.html">4.7.1.4 StreamReaderWriter Objects</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H3>
<BR>
4.7.1.3 StreamReader Objects
</H3>
<P>
The <tt class="class">StreamReader</tt> class is a subclass of <tt class="class">Codec</tt> and
defines the following methods which every stream reader must define in
order to be compatible to the Python codec registry.
<P>
<dl><dt><b><a name='l2h-667'><tt class='class'>StreamReader</tt></a></b> (<var>stream</var><big>[</big><var>, errors</var><big>]</big>)
<dd>
Constructor for a <tt class="class">StreamReader</tt> instance.
<P>
All stream readers must provide this constructor interface. They are
free to add additional keyword arguments, but only the ones defined
here are used by the Python codec registry.
<P>
<var>stream</var> must be a file-like object open for reading (binary)
data.
<P>
The <tt class="class">StreamReader</tt> may implement different error handling
schemes by providing the <var>errors</var> keyword argument. These
parameters are defined:
<P>
<UL>
<LI><code>'strict'</code> Raise <tt class="exception">ValueError</tt> (or a subclass);
this is the default.
</LI>
<LI><code>'ignore'</code> Ignore the character and continue with the next.
</LI>
<LI><code>'replace'</code> Replace with a suitable replacement character.
</LI>
</UL>
</dl>
<P>
<dl><dt><b><a name='l2h-668'><tt class='method'>read</tt></a></b> (<big>[</big><var>size</var><big>]</big>)
<dd>
Decodes data from the stream and returns the resulting object.
<P>
<var>size</var> indicates the approximate maximum number of bytes to read
from the stream for decoding purposes. The decoder can modify this
setting as appropriate. The default value -1 indicates to read and
decode as much as possible. <var>size</var> is intended to prevent having
to decode huge files in one step.
<P>
The method should use a greedy read strategy meaning that it should
read as much data as is allowed within the definition of the encoding
and the given size, e.g. if optional encoding endings or state
markers are available on the stream, these should be read too.
</dl>
<P>
<dl><dt><b><a name='l2h-669'><tt class='method'>readline</tt></a></b> (<var>[size]</var>)
<dd>
Read one line from the input stream and return the
decoded data.
<P>
Note: Unlike the <tt class="method">readlines()</tt> method, this method inherits
the line breaking knowledge from the underlying stream's
<tt class="method">readline()</tt> method - there is currently no support for line
breaking using the codec decoder due to lack of line buffering.
Sublcasses should however, if possible, try to implement this method
using their own knowledge of line breaking.
<P>
<var>size</var>, if given, is passed as size argument to the stream's
<tt class="method">readline()</tt> method.
</dl>
<P>
<dl><dt><b><a name='l2h-670'><tt class='method'>readlines</tt></a></b> (<var>[sizehint]</var>)
<dd>
Read all lines available on the input stream and return them as list
of lines.
<P>
Line breaks are implemented using the codec's decoder method and are
included in the list entries.
<P>
<var>sizehint</var>, if given, is passed as <var>size</var> argument to the
stream's <tt class="method">read()</tt> method.
</dl>
<P>
<dl><dt><b><a name='l2h-671'><tt class='method'>reset</tt></a></b> ()
<dd>
Resets the codec buffers used for keeping state.
<P>
Note that no stream repositioning should take place. This method is
primarily intended to be able to recover from decoding errors.
</dl>
<P>
In addition to the above methods, the <tt class="class">StreamReader</tt> must also
inherit all other methods and attribute from the underlying stream.
<P>
The next two base classes are included for convenience. They are not
needed by the codec registry, but may provide useful in practice.
<P>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="stream-writer-objects.html" tppabs="http://www.python.org/doc/current/lib/stream-writer-objects.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="node80.html" tppabs="http://www.python.org/doc/current/lib/node80.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="stream-reader-writer.html" tppabs="http://www.python.org/doc/current/lib/stream-reader-writer.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="stream-writer-objects.html" tppabs="http://www.python.org/doc/current/lib/stream-writer-objects.html">4.7.1.2 StreamWriter Objects</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="node80.html" tppabs="http://www.python.org/doc/current/lib/node80.html">4.7.1 Codec Base Classes</A>
<b class="navlabel">Next:</b> <a class="sectref" href="stream-reader-writer.html" tppabs="http://www.python.org/doc/current/lib/stream-reader-writer.html">4.7.1.4 StreamReaderWriter Objects</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -