Search code examples
pythonpython-2.7dictionaryunicodepython-c-api

Unicode key for Python dictionary in Python C API


I am using Python C API to connect to my python v2.7.2

As the title suggests, I am looking to use unicode string as key in my dictionary. I am aware that we can use unicode string as key in python dictionary. But how is that possible through Python C API.

int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)

we have the above for using the ascii values. Is there any way for it?

Thanks in advance.


Solution

  • Use

    int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)

    and pass in a:

    PyObject *PyUnicode_FromString(const char *u)

    as the key