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

📄 50.html

📁 国外python经典教材,python爱好者的首选
💻 HTML
字号:

<!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; Unicode Strings</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="42.html" class="navtitle">4. Operators and Expressions</a> &gt; <span class="nonavtitle">Unicode Strings</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="49.html" title="Type Conversion"><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=50" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="50.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="51.html" title="Boolean Expressions and Truth Values"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+8%3A56%3A43+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238110137104030195073070001013164</font><a href="read8.asp?bookname=0735710910&snode=50&now=5%2F28%2F2002+8%3A56%3A43+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Unicode Strings</h3>
<p>The use of standard strings and Unicode strings in the same program presents a number of subtle complications. This is because such strings may be used in a variety of operations, including string concatenation, comparisons, dictionary key lookups, and as arguments to built-in functions.</p>

<P>To convert a standard string <I><TT clasS="monofont">s</TT></I>
 to a Unicode string, the built-in function <tt clASS="monofont">unicode(</Tt><i><tt class="monofont">s</tt></i>
<tt class="monofont"> [, </tT><i><tT claSs="monofont">encoding</tt></i>
<Tt clASS="monofont"> [,</Tt><i><tt CLASs="monofont">errors</tt></i>
<TT CLass="monofont">]])</tT> is used. To convert a Unicode string <I><TT class="monofont">u</tt></i>
 to a standard string, the string method <i><tt class="monofont">u</tt></i>
<tT clAss="monofont">.encode([</tT><i><tt cLass="monofont">encoding</TT></I>
<Tt claSS="monofont"> [, </TT><i><tt cLASS="monofont">errors</tt></i>
<tT CLAss="monofont">]])</tt> is used. Both of these conversion operators require the use of a special encoding rule that specifies how 16-bit Unicode character values are mapped to a sequence of 8-bit characters in standard strings and vice versa. The encoding parameter is specified as a string and consists of one of the following values:</p>

<p><table border="1" celLspAcinG="0" cellPaddING="1" Width="100%">
<COLGroup SPAN="2">
<tr>
<th VALIgn="top">
<font size="2">
<p><b>Value</b></p>
</font></th>
<Th vAligN="top">
<font Size="2">
<P><B>Description</B></P>
</font></TH>
</TR>
<tr>
<td VALIgn="top">
<foNT SIze="2">
<p><tt class="monofont">'ascii'</tt></p>
</font></td>
<tD vaLign="top">
<Font sIze="2">
<p>7-bit ASCII</P>
</FONt></td>
</tR>
<TR>
<Td valIGN="top">
<Font sIZE="2">
<P><tt class="monofont">'latin-1' or 'iso-8859-1'</tt></p>
</font></td>
<td vaLigN="top">
<fonT size="2">
<P>ISO 8859-1 Latin-1</p>
</foNT></TD>
</tr>
<tr>
<TD VAlign="top">
<FONT size="2">
<P><TT Class="monofont">'utf-8'</tt></p>
</font></td>
<td valigN="top">
<foNt siZe="2">
<p>8-bit variable-length encoding</p>
</fOnt></tD>
</TR>
<Tr>
<td vALIGn="top">
<fonT SIZe="2">
<p><tt CLASs="monofont">'utf-16'</tt></p>
</font></td>
<td valign="top">
<foNt sIze="2">
<p>16-bit variable-length encoding (may be little or big endian)</P>
</font></Td>
</tr>
<TR>
<TD valiGN="top">
<FOnt siZE="2">
<P><Tt claSS="monofont">'utf-16-le'</TT></p>
</font></td>
<td valign="top">
<font sIze="2">
<P>UTF-16, little-endian encoding</p>
</foNt></td>
</tR>
<tr>
<tD VALign="top">
<fONT Size="2">
<p><TT CLass="monofont">'utf-16-be'</tT></P>
</FOnt></td>
<td valign="top">
<font size="2">
<P>UTF-16, big-endian encoding</p>
</fOnt></tD>
</tr>
<tr>
<Td vaLIGN="top">
<font SIZE="2">
<p><tt cLASS="monofont">'unicode-escape'</tt></p>
</fONT></Td>
<td valign="top">
<font size="2">
<p>Same format as Unicode literals <tt ClaSs="monofont">u"string"</tt></P>
</font></Td>
</tr>
<TR>
<TD valiGN="top">
<FOnt siZE="2">
<P><Tt claSS="monofont">'raw-unicode-escape'</TT></p>
</font></td>
<td valign="top">
<font sIze="2">
<P>Same format as raw Unicode literals <tt cLass="monofont">ur"string"</tT></p>
</foNT></TD>
</tr>
</coLGROup>
</taBLE></P>

<p>The default encoding is set in the <tt cLASS="monofont">site</tt>  module and can be queried using <tt class="monofont">sys.getdefaultencoding()</tt>. In most cases, the default encoding is <tt class="monofont">'ascii'</tt>, which means that ASCII characters with values in the range [0x00,0x7f] are directly mapped to Unicode characters in the range [U+0000, U+007F]. Details about the other encodings can be found in <A hrEf="89.html">Chapter 9</a>, 揑nput and Output.

⌨️ 快捷键说明

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