新聞中心
字符串轉(zhuǎn)換與格式化
用于數(shù)字轉(zhuǎn)換和格式化字符串輸出的函數(shù)

創(chuàng)新互聯(lián)主營相城網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,APP應(yīng)用開發(fā),相城h5微信小程序定制開發(fā)搭建,相城網(wǎng)站營銷推廣歡迎相城等地區(qū)企業(yè)咨詢
int PyOS_snprintf(char *str, size_t size, const char *format, …)
Part of the Stable ABI.
根據(jù)格式字符串 format 和額外參數(shù),輸出不超過 size 個字節(jié)到 str。 參見 Unix 手冊頁面 snprintf(3))。
int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
Part of the Stable ABI.
根據(jù)格式字符串 format 和變量參數(shù)列表 va,輸出不超過 size 個字節(jié)到 str。 參見 Unix 手冊頁面 vsnprintf(3))。
PyOS_snprintf() 和 PyOS_vsnprintf() 包裝 C 標(biāo)準(zhǔn)庫函數(shù) snprintf() 和 vsnprintf() 。它們的目的是保證在極端情況下的一致行為,而標(biāo)準(zhǔn) C 的函數(shù)則不然。
The wrappers ensure that str[size-1] is always '\0' upon return. They never write more than size bytes (including the trailing '\0') into str. Both functions require that str != NULL, size > 0, format != NULL and size < INT_MAX. Note that this means there is no equivalent to the C99 n = snprintf(NULL, 0, ...) which would determine the necessary buffer size.
這些函數(shù)的返回值( rv )應(yīng)按照以下規(guī)則被解釋:
當(dāng)
0 <= rv < size時,輸出轉(zhuǎn)換即成功并將 rv 個字符寫入到 str (不包括末尾str[rv]位置的'\0'字節(jié))。當(dāng)
rv >= size時,輸出轉(zhuǎn)換會被截斷并且需要一個具有rv + 1字節(jié)的緩沖區(qū)才能成功執(zhí)行。 在此情況下str[size-1]為'\0'。當(dāng)
rv < 0時,”會發(fā)生不好的事情?!?在此情況下str[size-1]也為'\0',但 str 的其余部分是未定義的。 錯誤的確切原因取決于底層平臺。
以下函數(shù)提供與語言環(huán)境無關(guān)的字符串到數(shù)字轉(zhuǎn)換。
double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception)
Part of the Stable ABI.
Convert a string s to a double, raising a python exception on failure. The set of accepted strings corresponds to the set of strings accepted by Python’s float() constructor, except that s must not have leading or trailing whitespace. The conversion is independent of the current locale.
如果 endptr 是 NULL ,轉(zhuǎn)換整個字符串。引發(fā) ValueError 并且 返回 -1.0 如果字符串不是浮點數(shù)的有效的表達(dá)方式。
如果 endptr 不是 NULL ,盡可能多的轉(zhuǎn)換字符串并將 *endptr 設(shè)置為指向第一個未轉(zhuǎn)換的字符。如果字符串的初始段不是浮點數(shù)的有效的表達(dá)方式,將 *endptr 設(shè)置為指向字符串的開頭,引發(fā) ValueError 異常,并且返回 -1.0 。
如果 s 表示一個太大而不能存儲在一個浮點數(shù)中的值(比方說, "1e500" 在許多平臺上是一個字符串)然后如果 overflow_exception 是 NULL 返回 Py_HUGE_VAL (用適當(dāng)?shù)姆枺┎⑶也辉O(shè)置任何異常。 在其他方面, overflow_exception 必須指向一個 Python 異常對象;引發(fā)異常并返回 -1.0 。在這兩種情況下,設(shè)置 *endptr 指向轉(zhuǎn)換值之后的第一個字符。
如果在轉(zhuǎn)換期間發(fā)生任何其他錯誤(比如一個內(nèi)存不足的錯誤),設(shè)置適當(dāng)?shù)?Python 異常并且返回 -1.0 。
3.1 新版功能.
char *PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)
Part of the Stable ABI.
Convert a double val to a string using supplied format_code, precision, and flags.
格式碼 必須是以下其中之一, 'e', 'E', 'f', 'F', 'g', 'G' 或者 'r'。對于 'r' , 提供的 精度 必須是0。'r' 格式碼指定了標(biāo)準(zhǔn)函數(shù) repr() 格式。
flags 可以為零或者其他值 Py_DTSF_SIGN, Py_DTSF_ADD_DOT_0 或 Py_DTSF_ALT 或其組合:
Py_DTSF_SIGN表示總是在返回的字符串前附加一個符號字符,即使 val 為非負(fù)數(shù)。Py_DTSF_ADD_DOT_0表示確保返回的字符串看起來不像是一個整數(shù)。Py_DTSF_ALT表示應(yīng)用 “替代的” 格式化規(guī)則。 相關(guān)細(xì)節(jié)請參閱 PyOS_snprintf()'#'定義文檔。
如果 ptype 不為 NULL,則它指向的值將被設(shè)為 Py_DTST_FINITE, Py_DTST_INFINITE 或 Py_DTST_NAN 中的一個,分別表示 val 是一個有限數(shù)字、無限數(shù)字或非數(shù)字。
返回值是一個指向包含轉(zhuǎn)換后字符串的 buffer 的指針,如果轉(zhuǎn)換失敗則為 NULL。 調(diào)用方要負(fù)責(zé)調(diào)用 PyMem_Free() 來釋放返回的字符串。
3.1 新版功能.
int PyOS_stricmp(const char *s1, const char *s2)
字符串不區(qū)分大小寫。該函數(shù)幾乎與 strcmp() 的工作方式相同,只是它忽略了大小寫。
int PyOS_strnicmp(const char *s1, const char *s2, Py_ssize_t size)
字符串不區(qū)分大小寫。該函數(shù)幾乎與 strncmp() 的工作方式相同,只是它忽略了大小寫。
本文名稱:創(chuàng)新互聯(lián)Python教程:字符串轉(zhuǎn)換與格式化
標(biāo)題網(wǎng)址:http://www.dlmjj.cn/article/dheidsg.html


咨詢
建站咨詢
