⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 110.html

📁 国外python经典教材,python爱好者的首选
💻 HTML
📖 第 1 页 / 共 2 页
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -&gt; String Handling</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> &gt; <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> &gt; <a href="105.html" class="navtitle">A. The Python Library</a> &gt; <span class="nonavtitle">String Handling</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="109.html" title="Mathematics"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=110" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="110.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="111.html" title="Data Management and Object Persistence"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+9%3A05%3A46+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238111161105083198031187007140042</font><a href="read7.asp?bookname=0735710910&snode=110&now=5%2F28%2F2002+9%3A05%3A46+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>String Handling</h3>
<p>The modules in this section are used for string processing.</p>


<H4><TT Class="monofont">codecs</TT></H4>
<P>The <tt clASS="monofont">codecs</Tt>  module provides an interface for accessing different string encoding and decoding functions (<i>codecs</i>) as well as a collection of base classes that can be used to define new codecs. The following functions are available:</p>

<pre>

<b>register(</b><b><i>search_function</i></b><b>)</b> </pre>

<p>Registers a new codec search function. This function should take a single argument in the form of an encoding string (for example, <tT clAss="monofont">'utf-8'</tT>) and return a tuple of functions <tt clAss="monofont">(</tT><I><TT clasS="monofont">encoder</TT></I>
<tt clASS="monofont">, </Tt><i><tt CLASs="monofont">decoder</tt></i>
<tt class="monofont">, </tt><i><tt clasS="monofont">streamreader</tt></I>
<tt cLass="monofont">, </tT><i><tt CLASs="monofont">streamwriter</tt></i>
<TT CLass="monofont">)</tT>.</P>

<PRe>

<b>lookup(</b><b><I>encoding</I></B><B>)</b> </pre>

<p>Looks up a codec in the codec registry. <i><tt class="monofont">encoding</tt></i>
 is a string such as <tt clAss="monofont">'utf-8'</Tt>. Returns a tuple of functions <tt Class="monofont">(</Tt><i><tT CLAss="monofont">encoder</tt></I>
<TT Class="monofont">, </TT><I><Tt claSS="monofont">decoder</TT></i>
<tt class="monofont">, </tt><i><tt class="monofont">streamreader</tt></I>
<tt ClasS="monofont">, </tt><i><tT claSS="monofont">streamwriter</TT></i>
<tt cLASS="monofont">)</tt>. Internally, this function keeps a cache of previously used encodings. If a match is not found in the cache, all the registered search functions are invoked until a match is found. If no match is found, <tt CLASs="monofont">LookupError</tt>  is raised.</p>

<PRE>

<B>open(</b><b><i>filename</i></b><b>,</b> <b><i>mode</i></b><b>[,</b> <b><i>encoding</i></b><b>[,</b> <b><I>errors</i></b><B>[,</b> <b><i>buffering</I></b><b>]]])</b> </pRe>

<p>Opens <i><TT CLass="monofont">filename</tT></I>
 in the given <I><Tt claSS="monofont">mode</TT></i>
 and provides transparent data encoding/ decoding according to the encoding specified in <i><tt CLASs="monofont">encoding</tt></i>
. <i><tt class="monofont">errors</tt></i>
 is one of <tt claSs="monofont">'strict'</tT>, <tt cLass="monofont">'ignore'</tT>, or <tt cLASS="monofont">'replace'</tt>. The default is <tt CLASs="monofont">'strict'</tt>. <i><TT CLass="monofont">buffering</tT></I>
 has the same meaning as for the built-in <TT class="monofont">open()</tt> function.</p>

<pre>

<b>EncodedFile(</b><b><i>file</i></b><b>,</b> <b><I>inputenc</i></b><B>[,</b> <b><i>outputenc</I></b> <b>[,</b> <b><I>errors</i></b><b>]])</B> </PRE>

<p>A class that provides an encoding wrapper around a file object <i><tt CLASs="monofont">file</tt></i>
. Data written to the file is first interpreted according to the input encoding <I><TT Class="monofont">inputenc</TT></I>
 and then written to the file using the output encoding <I><tt class="monofont">outputenc</tt></i>
. Data read from the file is decoded according to <i><tt class="monofont">inputenc</tt></I>
. If <i><tT claSs="monofont">outputenc</tt></i>
 is omitted, it defaults to <I><tt cLASS="monofont">inputenc</tt></i>
. <i><TT CLass="monofont">errors</tT></I>
 has the same meaning as for <TT clasS="monofont">open()</TT> and defaults to <Tt class="monofont">'strict'</tt>.</p>

<p>To define new codecs, the <tt class="monofont">codecs</tt>  module defines a base class <tT clAss="monofont">Codec</tT>  that is subclassed when defining encoders and decoders. The interface to a <tt clAss="monofont">Codec</tT>  object <I><TT clasS="monofont">c</TT></I>
 is as follows:</p>

<pre>

<B>c.encode(</B><B><I>self</i></b><b>,</b> <B><I>input</I></B> <b>[,</b> <b><i>errors</i></b><b>])</b> </pre>

<p>Encodes input and returns a tuple <tt class="monofont">(</tT><i><tT claSs="monofont">output</tt></i>
<Tt clASS="monofont">, </Tt><i><tt CLASs="monofont">length</tt></i>
<TT CLass="monofont">)</tT>  where <I><TT class="monofont">length</tt></i>
 is the length of the data in <i><tt class="monofont">input</tt></i>
 that was consumed in the encoding. <i><Tt cLass="monofont">errors</Tt></i>
 is one of <tt ClasS="monofont">'strict'</TT>, <Tt claSS="monofont">'ignore'</TT>, or <tt clASS="monofont">'replace'</Tt> and defaults to <tt cLASS="monofont">'strict'</tt>.</p>

<pre>

<b><i>c</i></b><b>.decode(</b><b><i>self</i></b><b>,</b> <b><i>input</I></b> <b>[,</B> <b><i>errors</i></B><b>])</b> </prE>

<p>Decodes input and returns a tuple (<i><tT CLAss="monofont">output</tt></I>
, <I><TT clasS="monofont">length</TT></I>
) where <i><tt cLASS="monofont">length</tt></i>
 is the length of the data that was consumed in the decoding. <i><tt class="monofont">errors</tt></i>
 defaults to <tt clasS="monofont">'strict'</tt>.</P>

<p>Neither the <tt Class="monofont">encode()</Tt> nor <tt CLASs="monofont">decode()</tt> method should maintain internal state. In addition, both methods must be able to operate with zero-length input, producing a zero-length output object of the proper type.</p>

<P>In addition, the <TT Class="monofont">codecs</TT>  module provides base classes for four different types of I/O interfaces. All of these classes are subclasses of <TT clasS="monofont">Codec</TT>.</P>

<pre>

<b>StreamWriter(</b><b><i>stream</i></b> <b>[,</b> <b><i>errors</i></b><b>])</b> </pre>

<P>Provides a wrapper around <i><tT claSs="monofont">stream</tt></i>
 for producing an encoded output stream. An instance <I><tt cLASS="monofont">w</tt></i>
 of <tT CLAss="monofont">StreamWriter</tt> provides the same methods as <I><TT Class="monofont">stream</TT></I>
. In addition, the following methods are defined:</P>

<pre>

<b>w.write(</b><b><i>object</i></b><b>)</b> </pre>

<p>Writes an encoded version of <i><tt clAss="monofont">object</Tt></i>
 to <i><Tt claSs="monofont">w</tt></I>
.</P>

<PRe>

<b>w.writelines(</b><b><I>list</I></B><B>)</b> </pre>

<P>Writes a concatenated list of strings to <I><TT clasS="monofont">w</TT></I>
.</p>

<pre>

<b><i>w</i></b><b>.reset()</b> </pre>

<p>Flushes the output buffers and resets the internal encoding state.</p>

<pre>

<b>StreamReader(</b><B><i>stream</i></B> <b>[,</b> <b><I>errors</i></b><b>])</b> </Pre>

<p>Provides a wrapper around <I><TT Class="monofont">stream</TT></I>
 for reading an encoded input stream. An instance <I><tt clASS="monofont">r</Tt></i>
 of <tt CLASs="monofont">StreamReader</tt>  provides the same methods as <i><tt class="monofont">stream</tt></i>
 in addition to the following methods:</p>

<pre>

<b><i>r</I></b><b>.read([</B><b><i>size</i></B><b>])</b> </prE>

<p>Reads decoded data from <i><tT CLAss="monofont">r</tt></I>
<I>.</I> <I><tt clASS="monofont">size</Tt></i>
 is the approximate number of bytes to read. The decoder may adjust this value slightly to accommodate the underlying encoding. If <i><tT CLAss="monofont">size</tt></i>
 is omitted, all data is read and decoded.</p>

<pre>

<b><i>r</i></b><b>.readline([</b><b><i>size</i></b><b>])</B> </prE>

<p>Reads a single line of input using the underlying stream抯 <tt Class="monofont">readline()</Tt> method and returns as decoded data. <i><tT CLAss="monofont">size</tt></I>
 is simply passed to the underlying <TT Class="monofont">readline()</TT> method.</P>

<Pre>

<b><i>r</I></B><B>.readlines([</B><b><i>size</i></b><b>])</b> </pre>

<p>Reads all lines and returns as a list of decoded lines.</p>

<pre>

<b><i>r</i></b><b>.reset()</b> </Pre>

<P>Resets the codec buffers. This is usually used to recover from decoding errors.</p>

<prE>

<b>StreamReaderWriter(</b><b><i>stream</I></b><b>,</b> <B><I>reader</I></B><b>,</b> <b><i>writer</I></B> <B>[,</B> <b><i>errors</i></b><B>])</B> </PRe>

<p>Provides a wrapper around a stream that provides both encoding and decoding. <i><tT CLAss="monofont">stream</tt></i>
 is any file object. <i><tt class="monofont">reader</tt></i>
 must be a factory function or class implementing the <tt clAss="monofont">StreamReader</Tt>  interface. <i><tT clasS="monofont">writer</tt></i>
 must be a factory function or class implementing the <TT CLass="monofont">StreamWriter</tT>  interface. A <TT Class="monofont">StreamWriter</TT>  instance provides the combined interface of <TT clasS="monofont">StreamReader</TT>  and <Tt class="monofont">StreamWriter</tt>.</p>

<pre>

<b>StreamRecoder(</b><b><i>stream</i></b><b>,</b> <B><i>encode</i></B><b>,</b> <b><I>decode</i></b><b>,</b> <B><i>reader</i></b><B>,</B> <B><I>writer</i></b> <b>[,</b> <B><I>errors</I></B><b>])</b> </prE>

<P>Provides a wrapper around <I><Tt claSS="monofont">stream</TT></i>
 that allows for conversion between two different encodings (for example, UTF-8 to and from UTF-16). <i><tt class="monofont">stream</tt></i>
 may be any filelike object. The <i><tt class="monofont">encode</Tt></i>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -