Hello,
Please i need help with python 2.7
i use "from string import Template"
and there error with Unicode
if i print the string without Template working good
and if i print it under Template appear error

""AH01215: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 8: ordinal not in range(128)""

my example :
2 files
index.py
template.py

in "template.py" file i use this code

#!/usr/bin/python
# -*- coding: utf-8 -*-
########################################################
#
from string import Template
ABC = Template("""<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Hello ${NAME}""")

and in index.py file i use this code

#!/usr/bin/python
# -*- coding: utf-8 -*-
########################################################
import template
print "Content-Type: text/html\n"
ZXC = "m’a réveillé"
print template.ABC.substitute(dict(NAME=ZXC))

if i used this code appear the error above
and if i print it direct without under template " print ZXC " working good

how can fix this utf8 under the template

Thanks

Recommended Answers

All 2 Replies

I'm not absolutely sure that line 6 with ZXC is a unicode string. It might need to be prefixed with the letter u as this example:
ZXC = u"m’a réveillé"

Thank you for reply, i find the issue in sql results must be add .encode('UTF-8') in it also

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.