gpt4 book ai didi

python - 我如何使用 pywintypes.Unicode()?

转载 作者:可可西里 更新时间:2023-11-01 10:20:02 26 4
gpt4 key购买 nike

如何在 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

我已经 seen其他 code用那个 look我也一样,所以这里有什么问题?

最佳答案

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的评论,并通过查看 PyArg_ParseTuple() 的文档页面来获得 Python 2Python 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com