如何在 Python 3.3.5 中使用 pywintypes.Unicode
?
import pywintypes
pywintypes.Unicode("s")
这会产生一个错误:
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
pywintypes.Unicode("s")
TypeError: must be impossible<bad format char>, not str
最佳答案
TL;DR:这是一个影响Python 3的错误,在Python 3中你不需要pywintypes.Unicode(text)
。只需使用text如果您需要字符串,则直接使用
;如果您需要字节形式,则直接使用 bytes(text,encoding)
。
错误
TypeError: must be impossible<bad format char>, not str
提示 C++ 源代码 t#
中的错误格式字符,这是不可能(未知)。
感谢eryksun的评论并查看 Python 2 的 PyArg_ParseTuple()
文档页面和 Python 3 ,很明显该错误位于 win32/src/PyWinTypesmodule.cpp .
PYWINTYPES_EXPORT PyObject *PyWin_NewUnicode(PyObject *self, PyObject *args)
{
char *string;
int slen;
if (!PyArg_ParseTuple(args, "t#", &string, &slen)) // <-- BUG: Python 2 format char
return NULL;
return PyUnicode_DecodeMBCS(string, slen, NULL);
}
t#
仅适用于 Python 2,对于 Python 3,它应该类似于 s*
,并且根据 eryksun 的说法,MBCS 解码是不必要的,因为 Python 已经处理了自动 Unicode 字符串。
关于python - 如何使用 pywintypes.Unicode()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31290641/