Pyrun_simplestring没有特殊字符 [英] Pyrun_simplestring failing for special characters

查看:587
本文介绍了Pyrun_simplestring没有特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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(\ n按任意键退出...... \ n);

如果(!_ getch())_getch();

返回0;



如果我只做



PyRun_SimpleString(txt = u\flag_for_Côte_d'Ivoire\)

它也失败了。我正在使用的Python版本是Python 3.7.3



我尝试过:



我有不同的编码技术。无法找到与PyRun_simplestring.Python版本相关的文档中的任何内容我使用的是Python3.7.3

I was trying Basics embedding Python in C++ and when i run this code i get error
"SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xf4 in position 10
: invalid continuation byte". I am not able to figure out what's wrong in this. I encoded this txt on Pycharm it worked perfectly fine but not when i try to embed this with C++. Thanking in advance for the help

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;

if i only do

PyRun_SimpleString("txt = u\"flag_for_Côte_d’Ivoire\"")
it fails for that too. Python Version which i am using is Python 3.7.3

What I have tried:

I have different Encoding techniques. Couldn't find anything in Docs related to PyRun_simplestring.Python version i am using is Python3.7.3

推荐答案

我不敢相信这可以在2但不在3所以,我只是用Python 3.7再试一次,它完美无缺。我注意到的是,与我昨天的测试不同的是,在包含Python.h之前添加了一行

Well I cannot believe that this works in 2 but not in 3. So, I just tried this again with Python 3.7 and it works perfectly. What I did notice, which was different from my tests yesterday, was the addition of the line
#define PY_SSIZE_T_CLEAN

,如简介 - Python 3.7.3文档 [ ^ ]。



所以我的源代码现在是:

before the include of Python.h, as described at Introduction — Python 3.7.3 documentation[^].

So my source code now reads:

#define PY_SSIZE_T_CLEAN
#include <Python.h>

int main()
{
//    PyObject* pInt;  -- not used

    Py_Initialize();

    PyRun_SimpleString("txt = u\"flag_for_Côte_d’Ivoire\"\nx = txt.encode()\nprint(x)");
    PyRun_SimpleString("txt = u\"flag_for_Côte_d’Ivoire\"\nprint(txt)"); // and text

    Py_Finalize();

    printf("\nPress any key to exit...\n");
}



这是输出:


And here is the output:

C:\Users\rjmac\Documents\Code\C++>pytest
b'flag_for_C\xc3\xb4te_d\xe2\x80\x99Ivoire'
flag_for_Côte_d’Ivoire

Press any key to exit...


在通话前转换它。在通话中这样做是没有意义的,因为它没有给你买任何东西而且你没有控制那个环境(关于那个编码的假设)。



Convert it before the call. There's no point in doing it in the call because it buys you nothing and you're not controlling "that" environment (regarding assumptions about that encoding).

引用:

当我发现流行的Web开发工具PHP几乎完全忽略了字符编码问题时,小心翼翼地使用8位字符,使其接近我认为,不可能开发出好的国际网络应用程序,足够了。

When I discovered that the popular web development tool PHP has almost complete ignorance of character encoding issues, blithely using 8 bits for characters, making it darn near impossible to develop good international web applications, I thought, enough is enough.





Absolute Minimum Every Software Developer Absolutely,肯定必须知道Unicode和字符集(没有借口!) - Joel on Software [ ^ ]


这篇关于Pyrun_simplestring没有特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆