Why does the following code print {b'a': 2} rather than {'a': 2}. And how can I fix it:
File1: parameters.pyx
# parameters.pyx
from libcpp.map cimport map
from libcpp.string cimport string
cdef class Parameters:
cdef map[string,int] data
cdef int myVal
def __init__(self):
self.data['a'] = 1
self.myVal = 0
cpdef void change_data(self, map[string,int] newdata):
self.data = newdata
cpdef map[string,int] print_data(self):
print(self.data)
cpdef void change_myVal(self):
self.myVal = 3
File2
# model.pyx
#from parameters import Parameters
from parameters import Parameters
from libcpp.map cimport map
from libcpp.string cimport string
cpdef single_run():
parameters = Parameters()
cdef map[string,int] newMap
newMap['a'] = 2
parameters.change_data(newMap)
parameters.print_data()
parameters.change_myVal()
Setup.py file
#!/usr/bin/env python3
from setuptools import setup
from Cython.Build import cythonize
setup(
ext_modules=cythonize(["*.py","*.pyx"],
language="c++")
)
You can directly use :
as the
b'a'is a byte object.If you want to convert it into string you can use
decode("utf-8")on byte object: