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

标签 python windows python-3.x unicode

如何在 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的评论并查看 Python 2PyArg_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/

相关文章:

c++ - Win32_NetworkAdapter 类为 boolean NetEnabled 返回 NULL;

java - 在 URL 上的 Conduit 上检测到 Winrm4j 授权循环,领域为 "null"

python - Adafruit MMA8451 加速计给我的 Raspberry Pi osError [Errno 121] 远程 I/O 错误

python - 有没有办法将变量发送到特定任务?

python - 是否可以通过请求找到此链接文本?

python - 版本控制- merge 两个分支-[中止: uncommitted changes]

java - 从 Java Servlet 模拟用户

python + 导入 C++ 编码帮助。 MSVCCompiler 缺少 compiler_cxx 修复程序;无法找到 vcvarsall.bat

python - 沿一维的 1D 数组和 3D 数组的高效乘积 - NumPy

python - 使用非 JSON 格式的表单数据抓取 ajax 调用