“Python/docs/3.9/c-api/conversion”的版本间差异

来自菜鸟教程
Python/docs/3.9/c-api/conversion
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:字符串转换和格式化 — Python 文档}}
 
<div id="string-conversion-and-formatting" class="section">
 
<div id="string-conversion-and-formatting" class="section">
  
 
<span id="string-conversion"></span>
 
<span id="string-conversion"></span>
= String conversion and formatting =
+
= 字符串转换和格式化 =
  
Functions for number conversion and formatted string output.
+
用于数字转换和格式化字符串输出的函数。
  
; int <code>PyOS_snprintf</code><span class="sig-paren">(</span>char *''str'', size_t ''size'', ''const'' char *''format'', ...<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyOS_snprintf</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">str</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">size</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">format</span></span>, <span class="p"><span class="pre">...</span></span><span class="sig-paren">)</span><br />
: Output not more than ''size'' bytes to ''str'' according to the format string ''format'' and the extra arguments. See the Unix man page ''[https://manpages.debian.org/snprintf(2) snprintf(2)]''.
 
  
; int <code>PyOS_vsnprintf</code><span class="sig-paren">(</span>char *''str'', size_t ''size'', ''const'' char *''format'', va_list ''va''<span class="sig-paren">)</span>
+
: 根据格式字符串 ''format'' 和额外的参数,输出不超过 ''size'' 个字节到 ''str''。 请参阅 Unix 手册页 ''snprintf(3)''
: Output not more than ''size'' bytes to ''str'' according to the format string ''format'' and the variable argument list ''va''. Unix man page ''[https://manpages.debian.org/vsnprintf(2) vsnprintf(2)]''.
 
  
[[#c.PyOS_snprintf|<code>PyOS_snprintf()</code>]] and [[#c.PyOS_vsnprintf|<code>PyOS_vsnprintf()</code>]] wrap the Standard C library
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyOS_vsnprintf</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">str</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">size</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">format</span></span>, <span class="n"><span class="pre">va_list</span></span><span class="w"> </span><span class="n"><span class="pre">va</span></span><span class="sig-paren">)</span><br />
functions <code>snprintf()</code> and <code>vsnprintf()</code>. Their purpose is to
 
guarantee consistent behavior in corner cases, which the Standard C functions do
 
not.
 
  
The wrappers ensure that ''str*[*size''-1] is always <code>'\0'</code> upon return. They
+
: 根据格式字符串''format''和变量参数列表''va''输出不超过''size''个字节到''str''。 Unix 手册页 ''vsnprintf(3)''
never write more than ''size'' bytes (including the trailing <code>'\0'</code>) into str.
 
Both functions require that <code>str != NULL</code>, <code>size &gt; 0</code> and <code>format != NULL</code>.
 
  
If the platform doesn't have <code>vsnprintf()</code> and the buffer size needed to
+
[[#c.PyOS_snprintf|PyOS_snprintf()]] 和 [[#c.PyOS_vsnprintf|PyOS_vsnprintf()]] 包装了标准 C 库函数 <code>snprintf()</code> <code>vsnprintf()</code>。 它们的目的是在极端情况下保证一致的行为,而标准 C 函数则没有。
avoid truncation exceeds ''size'' by more than 512 bytes, Python aborts with a
 
[[../sys#c|<code>Py_FatalError()</code>]].
 
  
The return value (''rv'') for these functions should be interpreted as follows:
+
包装器确保 <code>str[size-1]</code> 在返回时始终为 <code>'\0'</code>。 他们从不向 str 写入超过 ''size'' 个字节(包括尾随的 <code>'\0'</code>)。 这两个函数都需要 <code>str != NULL</code>、<code>size &gt; 0</code> 和 <code>format != NULL</code>。
  
* When <code>0 &lt;= rv &lt; size</code>, the output conversion was successful and ''rv'' characters were written to ''str'' (excluding the trailing <code>'\0'</code> byte at ''str*[*rv'']).
+
如果平台没有 <code>vsnprintf()</code> 并且避免截断所需的缓冲区大小超过 ''size'' 超过 512 个字节,则 Python 会以 [[../sys#c|Py_FatalError()]] 中止。
* When <code>rv &gt;= size</code>, the output conversion was truncated and a buffer with <code>rv + 1</code> bytes would have been needed to succeed. ''str*[*size''-1] is <code>'\0'</code> in this case.
 
* When <code>rv &lt; 0</code>, &quot;something bad happened.&quot; ''str*[*size''-1] is <code>'\0'</code> in this case too, but the rest of ''str'' is undefined. The exact cause of the error depends on the underlying platform.
 
  
The following functions provide locale-independent string to number conversions.
+
这些函数的返回值 (''rv'') 应解释如下:
 +
 
 +
* 当<code>0 &lt;= rv &lt; size</code>时,输出转换成功,''rv''字符被写入''str''(不包括<code>str[rv]</code>处的尾随<code>'\0'</code>字节)。
 +
* 当 <code>rv &gt;= size</code> 时,输出转换被截断,并且需要具有 <code>rv + 1</code> 字节的缓冲区才能成功。 在这种情况下,<code>str[size-1]</code> 是 <code>'\0'</code>。
 +
* 当<code>rv &lt; 0</code>时,“发生了不好的事情。” <code>str[size-1]</code> 在这种情况下也是 <code>'\0'</code>,但 ''str'' 的其余部分未定义。 错误的确切原因取决于底层平台。
 +
 
 +
以下函数提供与语言环境无关的字符串到数字的转换。
  
 
<dl>
 
<dl>
<dt>double <code>PyOS_string_to_double</code><span class="sig-paren">(</span>''const'' char *''s'', char **''endptr'', [[../structures#c|PyObject]] *''overflow_exception''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyOS_string_to_double</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">s</span></span>, <span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">endptr</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">overflow_exception</span></span><span class="sig-paren">)</span><br />
<dd><p>Convert a string <code>s</code> to a <span class="xref c c-texpr">double</span>, raising a Python
+
</dt>
exception on failure. The set of accepted strings corresponds to
+
<dd><p>将字符串 <code>s</code> 转换为 <span class="xref c c-texpr">double</span>,在失败时引发 Python 异常。 接受的字符串集对应于 Python [[../../library/functions#float|float()]] 构造函数接受的字符串集,除了 <code>s</code> 不能有前导或尾随空格。 转换与当前语言环境无关。</p>
the set of strings accepted by Python's [[../../library/functions#float|<code>float()</code>]] constructor,
+
<p>如果 <code>endptr</code> <code>NULL</code>,则转换整个字符串。 如果字符串不是浮点数的有效表示,则引发 [[../../library/exceptions#ValueError|ValueError]] 并返回 <code>-1.0</code></p>
except that <code>s</code> must not have leading or trailing whitespace.
+
<p>如果 endptr 不是 <code>NULL</code>,则尽可能多地转换字符串并将 <code>*endptr</code> 设置为指向第一个未转换的字符。 如果没有字符串的初始段是浮点数的有效表示,则将 <code>*endptr</code> 设置为指向字符串的开头,引发 ValueError,并返回 <code>-1.0</code></p>
The conversion is independent of the current locale.</p>
+
<p>如果 <code>s</code> 表示的值太大而无法存储在浮点数中(例如,<code>&quot;1e500&quot;</code> 在许多平台上都是这样的字符串),那么如果 <code>overflow_exception</code> 是 [ X155X] 返回 <code>Py_HUGE_VAL</code>(带有适当的符号)并且不设置任何异常。 否则,<code>overflow_exception</code> 必须指向一个 Python 异常对象; 引发该异常并返回 <code>-1.0</code>。 在这两种情况下,将 <code>*endptr</code> 设置为指向转换值后的第一个字符。</p>
<p>If <code>endptr</code> is <code>NULL</code>, convert the whole string. Raise
+
<p>如果在转换过程中发生任何其他错误(例如内存不足错误),请设置适当的 Python 异常并返回 <code>-1.0</code></p>
[[../../library/exceptions#ValueError|<code>ValueError</code>]] and return <code>-1.0</code> if the string is not a valid
 
representation of a floating-point number.</p>
 
<p>If endptr is not <code>NULL</code>, convert as much of the string as
 
possible and set <code>*endptr</code> to point to the first unconverted
 
character. If no initial segment of the string is the valid
 
representation of a floating-point number, set <code>*endptr</code> to point
 
to the beginning of the string, raise ValueError, and return
 
<code>-1.0</code>.</p>
 
<p>If <code>s</code> represents a value that is too large to store in a float
 
(for example, <code>&quot;1e500&quot;</code> is such a string on many platforms) then
 
if <code>overflow_exception</code> is <code>NULL</code> return <code>Py_HUGE_VAL</code> (with
 
an appropriate sign) and don't set any exception. Otherwise,
 
<code>overflow_exception</code> must point to a Python exception object;
 
raise that exception and return <code>-1.0</code>. In both cases, set
 
<code>*endptr</code> to point to the first character after the converted value.</p>
 
<p>If any other error occurs during the conversion (for example an
 
out-of-memory error), set the appropriate Python exception and
 
return <code>-1.0</code>.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.1 新版功能.</span></p>
+
<p><span class="versionmodified added">3.1 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>char *<code>PyOS_double_to_string</code><span class="sig-paren">(</span>double ''val'', char ''format_code'', int ''precision'', int ''flags'', int *''ptype''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyOS_double_to_string</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="n"><span class="pre">val</span></span>, <span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="n"><span class="pre">format_code</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">precision</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">flags</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">ptype</span></span><span class="sig-paren">)</span><br />
<dd><p>Convert a <span class="xref c c-texpr">double</span> ''val'' to a string using supplied
+
</dt>
''format_code'', ''precision'', and ''flags''.</p>
+
<dd><p>使用提供的 ''format_code''、''precision'' 和 ''flags'' 将 <span class="xref c c-texpr">double</span> ''val'' 转换为字符串。</p>
<p>''format_code'' must be one of <code>'e'</code>, <code>'E'</code>, <code>'f'</code>, <code>'F'</code>,
+
<p>''format_code'' 必须是 <code>'e'</code><code>'E'</code><code>'f'</code><code>'F'</code><code>'g'</code>、[ X95X] 或 <code>'r'</code>。 对于 <code>'r'</code>,提供的 ''precision'' 必须为 0 且被忽略。 <code>'r'</code> 格式代码指定标准的 [[../../library/functions#repr|repr()]] 格式。</p>
<code>'g'</code>, <code>'G'</code> or <code>'r'</code>. For <code>'r'</code>, the supplied ''precision''
+
<p>''flags'' 可以是值 <code>Py_DTSF_SIGN</code><code>Py_DTSF_ADD_DOT_0</code> <code>Py_DTSF_ALT</code> 中的零个或多个,或一起计算:</p>
must be 0 and is ignored. The <code>'r'</code> format code specifies the
 
standard [[../../library/functions#repr|<code>repr()</code>]] format.</p>
 
<p>''flags'' can be zero or more of the values <code>Py_DTSF_SIGN</code>,
 
<code>Py_DTSF_ADD_DOT_0</code>, or <code>Py_DTSF_ALT</code>, or-ed together:</p>
 
 
<ul>
 
<ul>
<li><p><code>Py_DTSF_SIGN</code> means to always precede the returned string with a sign
+
<li><p><code>Py_DTSF_SIGN</code> 意味着总是在返回的字符串前面加上一个符号字符,即使 ''val'' 是非负的。</p></li>
character, even if ''val'' is non-negative.</p></li>
+
<li><p><code>Py_DTSF_ADD_DOT_0</code> 表示确保返回的字符串不会像整数。</p></li>
<li><p><code>Py_DTSF_ADD_DOT_0</code> means to ensure that the returned string will not look
+
<li><p><code>Py_DTSF_ALT</code> 表示应用“替代”格式规则。 有关详细信息,请参阅 [[#c.PyOS_snprintf|PyOS_snprintf()]] <code>'#'</code> 说明符的文档。</p></li></ul>
like an integer.</p></li>
 
<li><p><code>Py_DTSF_ALT</code> means to apply &quot;alternate&quot; formatting rules. See the
 
documentation for the [[#c.PyOS_snprintf|<code>PyOS_snprintf()</code>]] <code>'#'</code> specifier for
 
details.</p></li></ul>
 
  
<p>If ''ptype'' is non-<code>NULL</code>, then the value it points to will be set to one of
+
<p>如果 ''ptype'' 是非 <code>NULL</code>,那么它指向的值将被设置为 <code>Py_DTST_FINITE</code><code>Py_DTST_INFINITE</code> <code>Py_DTST_NAN</code> 之一],分别表示''val''为有限数、无限数或非数。</p>
<code>Py_DTST_FINITE</code>, <code>Py_DTST_INFINITE</code>, or <code>Py_DTST_NAN</code>, signifying that
+
<p>如果转换失败,则返回值是指向 ''buffer'' 的指针以及转换后的字符串或 <code>NULL</code>。 调用者负责通过调用 [[../memory#c|PyMem_Free()]] 来释放返回的字符串。</p>
''val'' is a finite number, an infinite number, or not a number, respectively.</p>
 
<p>The return value is a pointer to ''buffer'' with the converted string or
 
<code>NULL</code> if the conversion failed. The caller is responsible for freeing the
 
returned string by calling [[../memory#c|<code>PyMem_Free()</code>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.1 新版功能.</span></p>
+
<p><span class="versionmodified added">3.1 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; int <code>PyOS_stricmp</code><span class="sig-paren">(</span>''const'' char *''s1'', ''const'' char *''s2''<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyOS_stricmp</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">s1</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">s2</span></span><span class="sig-paren">)</span><br />
: Case insensitive comparison of strings. The function works almost identically to <code>strcmp()</code> except that it ignores the case.
+
 
 +
: 字符串不区分大小写的比较。 该函数的工作原理与 <code>strcmp()</code> 几乎相同,只是它忽略了大小写。
 +
 
 +
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyOS_strnicmp</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">s1</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">s2</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">size</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 字符串不区分大小写的比较。 该函数的工作原理与 <code>strncmp()</code> 几乎相同,只是它忽略了大小写。
 +
 
 +
 
 +
</div>
 +
<div class="clearer">
  
; int <code>PyOS_strnicmp</code><span class="sig-paren">(</span>''const'' char *''s1'', ''const'' char *''s2'', Py_ssize_t ''size''<span class="sig-paren">)</span>
 
: Case insensitive comparison of strings. The function works almost identically to <code>strncmp()</code> except that it ignores the case.
 
  
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:50的最新版本

字符串转换和格式化

用于数字转换和格式化字符串输出的函数。

int PyOS_snprintf(char *str, size_t size, const char *format, ...)
根据格式字符串 format 和额外的参数,输出不超过 size 个字节到 str。 请参阅 Unix 手册页 snprintf(3)
int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
根据格式字符串format和变量参数列表va输出不超过size个字节到str。 Unix 手册页 vsnprintf(3)

PyOS_snprintf()PyOS_vsnprintf() 包装了标准 C 库函数 snprintf()vsnprintf()。 它们的目的是在极端情况下保证一致的行为,而标准 C 函数则没有。

包装器确保 str[size-1] 在返回时始终为 '\0'。 他们从不向 str 写入超过 size 个字节(包括尾随的 '\0')。 这两个函数都需要 str != NULLsize > 0format != NULL

如果平台没有 vsnprintf() 并且避免截断所需的缓冲区大小超过 size 超过 512 个字节,则 Python 会以 Py_FatalError() 中止。

这些函数的返回值 (rv) 应解释如下:

  • 0 <= rv < size时,输出转换成功,rv字符被写入str(不包括str[rv]处的尾随'\0'字节)。
  • rv >= size 时,输出转换被截断,并且需要具有 rv + 1 字节的缓冲区才能成功。 在这种情况下,str[size-1]'\0'
  • rv < 0时,“发生了不好的事情。” str[size-1] 在这种情况下也是 '\0',但 str 的其余部分未定义。 错误的确切原因取决于底层平台。

以下函数提供与语言环境无关的字符串到数字的转换。

double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception)

将字符串 s 转换为 double,在失败时引发 Python 异常。 接受的字符串集对应于 Python 的 float() 构造函数接受的字符串集,除了 s 不能有前导或尾随空格。 转换与当前语言环境无关。

如果 endptrNULL,则转换整个字符串。 如果字符串不是浮点数的有效表示,则引发 ValueError 并返回 -1.0

如果 endptr 不是 NULL,则尽可能多地转换字符串并将 *endptr 设置为指向第一个未转换的字符。 如果没有字符串的初始段是浮点数的有效表示,则将 *endptr 设置为指向字符串的开头,引发 ValueError,并返回 -1.0

如果 s 表示的值太大而无法存储在浮点数中(例如,"1e500" 在许多平台上都是这样的字符串),那么如果 overflow_exception 是 [ X155X] 返回 Py_HUGE_VAL(带有适当的符号)并且不设置任何异常。 否则,overflow_exception 必须指向一个 Python 异常对象; 引发该异常并返回 -1.0。 在这两种情况下,将 *endptr 设置为指向转换值后的第一个字符。

如果在转换过程中发生任何其他错误(例如内存不足错误),请设置适当的 Python 异常并返回 -1.0

3.1 版中的新功能。

char *PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)

使用提供的 format_codeprecisionflagsdouble val 转换为字符串。

format_code 必须是 'e''E''f''F''g'、[ X95X] 或 'r'。 对于 'r',提供的 precision 必须为 0 且被忽略。 'r' 格式代码指定标准的 repr() 格式。

flags 可以是值 Py_DTSF_SIGNPy_DTSF_ADD_DOT_0Py_DTSF_ALT 中的零个或多个,或一起计算:

  • Py_DTSF_SIGN 意味着总是在返回的字符串前面加上一个符号字符,即使 val 是非负的。

  • Py_DTSF_ADD_DOT_0 表示确保返回的字符串不会像整数。

  • Py_DTSF_ALT 表示应用“替代”格式规则。 有关详细信息,请参阅 PyOS_snprintf() '#' 说明符的文档。

如果 ptype 是非 NULL,那么它指向的值将被设置为 Py_DTST_FINITEPy_DTST_INFINITEPy_DTST_NAN 之一],分别表示val为有限数、无限数或非数。

如果转换失败,则返回值是指向 buffer 的指针以及转换后的字符串或 NULL。 调用者负责通过调用 PyMem_Free() 来释放返回的字符串。

3.1 版中的新功能。

int PyOS_stricmp(const char *s1, const char *s2)
字符串不区分大小写的比较。 该函数的工作原理与 strcmp() 几乎相同,只是它忽略了大小写。
int PyOS_strnicmp(const char *s1, const char *s2, Py_ssize_t size)
字符串不区分大小写的比较。 该函数的工作原理与 strncmp() 几乎相同,只是它忽略了大小写。