📄 module-locale.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>6.22 locale -- Internationalization services</title>
<META NAME="description" CONTENT="6.22 locale -- Internationalization services">
<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="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html">
<LINK REL="previous" href="module-shutil.html" tppabs="http://www.python.org/doc/current/lib/module-shutil.html">
<LINK REL="up" href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html">
<LINK REL="next" HREF="node141.html" tppabs="http://www.python.org/doc/current/lib/node141.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="shutil-example.html" tppabs="http://www.python.org/doc/current/lib/shutil-example.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="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.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="node141.html" tppabs="http://www.python.org/doc/current/lib/node141.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="shutil-example.html" tppabs="http://www.python.org/doc/current/lib/shutil-example.html">6.21.1 Example</A>
<b class="navlabel">Up:</b> <a class="sectref" href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html">6. Generic Operating System</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node141.html" tppabs="http://www.python.org/doc/current/lib/node141.html">6.22.1 Background, details, hints,</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0082200000000000000000">
6.22 <tt class="module">locale</tt> --
Internationalization services</A>
</H1>
<P>
<P>
The <tt class="module">locale</tt> module opens access to the POSIX locale database
and functionality. The POSIX locale mechanism allows programmers
to deal with certain cultural issues in an application, without
requiring the programmer to know all the specifics of each country
where the software is executed.
<P>
The <tt class="module">locale</tt> module is implemented on top of the
<tt class="module">_locale</tt> module, which in turn uses an
ANSI C locale implementation if available.
<P>
The <tt class="module">locale</tt> module defines the following exception and
functions:
<P>
<dl><dt><b><a name='l2h-1492'><tt class='function'>setlocale</tt></a></b> (<var>category</var><big>[</big><var>, value</var><big>]</big>)
<dd>
If <var>value</var> is specified, modifies the locale setting for the
<var>category</var>. The available categories are listed in the data
description below. The value is the name of a locale. An empty string
specifies the user's default settings. If the modification of the
locale fails, the exception <tt class="exception">Error</tt> is
raised. If successful, the new locale setting is returned.
<P>
If no <var>value</var> is specified, the current setting for the
<var>category</var> is returned.
<P>
<tt class="function">setlocale()</tt> is not thread safe on most systems. Applications
typically start with a call of
<dl><dd><pre class="verbatim">
import locale
locale.setlocale(locale.LC_ALL,"")
</pre></dl>
This sets the locale for all categories to the user's default setting
(typically specified in the <a class="envvar" name='l2h-1510'>$LANG</a> environment variable). If
the locale is not changed thereafter, using multithreading should not
cause problems.
</dl>
<P>
<dl><dt><b><a name='l2h-1493'><tt class='exception'>Error</tt></a></b>
<dd>
Exception raised when <tt class="function">setlocale()</tt> fails.
</dl>
<P>
<dl><dt><b><a name='l2h-1494'><tt class='function'>localeconv</tt></a></b> ()
<dd>
Returns the database of of the local conventions as a dictionary. This
dictionary has the following strings as keys:
<UL>
<LI><code>decimal_point</code> specifies the decimal point used in
floating point number representations for the <tt class="constant">LC_NUMERIC</tt>
category.
</LI>
<LI><code>grouping</code> is a sequence of numbers specifying at which
relative positions the <code>thousands_sep</code> is expected. If the
sequence is terminated with <tt class="constant">CHAR_MAX</tt>, no further
grouping is performed. If the sequence terminates with a <code>0</code>, the last
group size is repeatedly used.
</LI>
<LI><code>thousands_sep</code> is the character used between groups.
</LI>
<LI><code>int_curr_symbol</code> specifies the international currency
symbol from the <tt class="constant">LC_MONETARY</tt> category.
</LI>
<LI><code>currency_symbol</code> is the local currency symbol.
</LI>
<LI><code>mon_decimal_point</code> is the decimal point used in monetary
values.
</LI>
<LI><code>mon_thousands_sep</code> is the separator for grouping of
monetary values.
</LI>
<LI><code>mon_grouping</code> has the same format as the <code>grouping</code>
key; it is used for monetary values.
</LI>
<LI><code>positive_sign</code> and <code>negative_sign</code> gives the sign
used for positive and negative monetary quantities.
</LI>
<LI><code>int_frac_digits</code> and <code>frac_digits</code> specify the number
of fractional digits used in the international and local formatting
of monetary values.
</LI>
<LI><code>p_cs_precedes</code> and <code>n_cs_precedes</code> specifies whether
the currency symbol precedes the value for positive or negative
values.
</LI>
<LI><code>p_sep_by_space</code> and <code>n_sep_by_space</code> specifies
whether there is a space between the positive or negative value and
the currency symbol.
</LI>
<LI><code>p_sign_posn</code> and <code>n_sign_posn</code> indicate how the
sign should be placed for positive and negative monetary values.
</LI>
</UL>
<P>
The possible values for <code>p_sign_posn</code> and
<code>n_sign_posn</code> are given below.
<P>
<table border align="center" style="border-collapse: collapse">
<thead>
<tr class="tableheader">
<th align="center"><b>Value</b> </th>
<th align="left"><b>Explanation</b> </th>
</thead>
<tbody valign='baseline'>
<tr><td align="center" valign="baseline"><code>0</code></td>
<td align="left">Currency and value are surrounded by parentheses.</td>
<tr><td align="center" valign="baseline"><code>1</code></td>
<td align="left">The sign should precede the value and currency symbol.</td>
<tr><td align="center" valign="baseline"><code>2</code></td>
<td align="left">The sign should follow the value and currency symbol.</td>
<tr><td align="center" valign="baseline"><code>3</code></td>
<td align="left">The sign should immediately precede the value.</td>
<tr><td align="center" valign="baseline"><code>4</code></td>
<td align="left">The sign should immediately follow the value.</td>
<tr><td align="center" valign="baseline"><code>LC_MAX</code></td>
<td align="left">Nothing is specified in this locale.</td></tbody>
</table>
</dl>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -