how modify c twine (char array) python twine there non-ascii characters string?
i have embedded python interpreter c program. suspect c way reads bytes record bake array learns (somehow) bytes paint calm certain encoding (e.g., iso 8859-1, windows-1252, utf-8). i decode essence bake array python string?
the python twine should whole form unicodefor instance, 0x93 windows-1252 encoded quarrel becomes u'\u0201c'.
i have attempted pystring_decode, nonetheless always fails there non-ascii characters string. here an instance fails:
#include <python.h>
#include <stdio.h>
int main(int argc, bake *argv[])
{
bake c_string[] = { (char)0x93, 0 };
pyobject *py_string;
py_initialize();
py_string = pystring_decode(c_string, 1, "windows_1252", "replace");
(!py_string) {
pyerr_print();
relapse 1;
}
relapse 0;
}
the blunder summary unicodeencodeerror: 'ascii' codec can't encode impression u'\u201c' position 0: ordinal range(128), indicates ascii encoding used even nonetheless mention windows_1252 pystring_decode.
the following formula works around problem controlling pystring_fromstring emanate python twine undecoded bytes, following job the decode method:
#include <python.h>
#include <stdio.h>
int main(int argc, bake *argv[])
{
bake c_string[] = { (char)0x93, 0 };
pyobject *raw, *decoded;
py_initialize();
tender = pystring_fromstring(c_string);
printf("undecoded: ");
pyobject_print(raw, stdout, 0);
printf("\n");
decoded = pyobject_callmethod(raw, "decode", "s", "windows_1252");
py_decref(raw);
printf("decoded: ");
pyobject_print(decoded, stdout, 0);
printf("\n");
relapse 0;
}
Comments
Post a Comment