2

int PyRun_SimpleString(const char *command) is the function which can run a command of Python.

I call this function in my program, but how can I specify the encoding of the command argument. I found that Python 3 seems to use UTF-8 as the default encoding when I run a command. Can I change this encoding in the C level API? ...or with an extra parameter when call PyRun_SimpleString-like function?

    1 Answer 1

    3

    You can prepend one of the "magic comments" described in PEP 263 to your command:

    char buf[1024]; snprintf(buf, sizeof(buf), "-*- coding: %s -*-\n", yourEncoding); strncat(buf, yourCommand, sizeof(buf) - strlen(buf) - 1); PyRun_SimpleString(buf); 
    1
    • But i think this is a little inconvenient , right? And i just want a way to specific the encoding at one time and i can use it for ever, is there this kind of function?
      – winterTTr
      CommentedNov 22, 2010 at 9:57

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.