PyRun_SimpleString 特殊字符失败
PyRun_SimpleString failing for special characters
我正在尝试在 C++ 中嵌入 Python 的基础知识,当我 运行 这段代码时出现错误
“语法错误:(unicode 错误)'utf-8' 编解码器无法解码位置 10 中的字节 0xf4
:无效的连续字节”。我无法弄清楚这有什么问题。我在 Pycharm 上编码了这个 txt 它工作得很好但是当我尝试用 C++ 嵌入它时却没有。提前感谢你的帮助
PyObject* pInt;
Py_Initialize();
PyRun_SimpleString("txt = u\"flag_for_Côte_d’Ivoire\"\nx = txt.encode()\nprint(x)");
Py_Finalize();
printf("\nPress any key to exit...\n");
if(!_getch()) _getch();
return 0;
如果我只做
PyRun_SimpleString("txt = u\"flag_for_Côte_d’Ivoire\"")
它也失败了。 Python 我使用的版本是 Python 3.7.3 和 windows VS 2008
在发送字符串 converted string to UTF8 and passed to Pyrun_simplestring()
之前成功了。因此,如果您正在使用旧版本的 VS,请按照说明操作,如果您正在使用新的 VS,则需要在 Python.h
之前添加“#define PY_SSIZE_T_CLEAN
”
注意:-此解决方案适用于那些正在 Python 3
上工作的人
我正在尝试在 C++ 中嵌入 Python 的基础知识,当我 运行 这段代码时出现错误 “语法错误:(unicode 错误)'utf-8' 编解码器无法解码位置 10 中的字节 0xf4 :无效的连续字节”。我无法弄清楚这有什么问题。我在 Pycharm 上编码了这个 txt 它工作得很好但是当我尝试用 C++ 嵌入它时却没有。提前感谢你的帮助
PyObject* pInt;
Py_Initialize();
PyRun_SimpleString("txt = u\"flag_for_Côte_d’Ivoire\"\nx = txt.encode()\nprint(x)");
Py_Finalize();
printf("\nPress any key to exit...\n");
if(!_getch()) _getch();
return 0;
如果我只做
PyRun_SimpleString("txt = u\"flag_for_Côte_d’Ivoire\"")
它也失败了。 Python 我使用的版本是 Python 3.7.3 和 windows VS 2008
在发送字符串 converted string to UTF8 and passed to Pyrun_simplestring()
之前成功了。因此,如果您正在使用旧版本的 VS,请按照说明操作,如果您正在使用新的 VS,则需要在 Python.h
#define PY_SSIZE_T_CLEAN
”
注意:-此解决方案适用于那些正在 Python 3
上工作的人