Consider following code to rendering data with mako:
The code may have following error, if the some_data contains non-ascii characters, because the implementation use directly use unicode method to convert some_data:
The error can be fixed by decode the some_data. Or there is another workaround, which is injecting a partial unicode function into the module generated:
from mako.template import Template tmpl = Template(filename=some_template_flle, module_directory=some_template_module_path) print tmpl.render(**some_data)
The code may have following error, if the some_data contains non-ascii characters, because the implementation use directly use unicode method to convert some_data:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
The error can be fixed by decode the some_data. Or there is another workaround, which is injecting a partial unicode function into the module generated:
from mako.template import Template
import functools tmpl = Template(filename=some_template_flle, module_directory=some_template_module_path)
tmpl.module.unicode = functools.partial(unicode, encoding='utf-8') print tmpl.render(**some_data)
Comments