2010-12-21 1 views
2

나는이 dummy.htmlUnicodeEncodeError 마코에서 템플릿

#!c:/Python27/python.exe -u 

from mako import exceptions 
from mako.template import Template 

print "Content-type: text/html" 
print 

#VARIABLE = "WE" 
VARIABLE = "我们" 
template = Template(filename='../template/dummy.html', output_encoding='utf8') 
try: 
    print template.render(VARIABLE=VARIABLE) 
except: 
    print exceptions.html_error_template().render() 

(UTF-8 형식으로 저장)

hello world 
哈罗世界 
${VARIABLE} 

dummy.py 다음 파일

나는의 지시를 심리했다.

그러나, 나는 아직도 내가 놓쳤다했다

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)

아무것도 오류 얻을?

답변

4
template = Template(filename='../template/dummy.html', default_filters=['decode.utf8'], input_encoding='utf-8', output_encoding='utf-8') 
2

예, ASCII로 렌더링하려고 했으므로 작동하지 않습니다.

Template(filename='../template/dummy.html', output_encoding='utf8') 

을 맨 손으로 excepts을하지 마십시오 : 당신은 output_encoding 사용할 무슨 말을해야합니다. 붙잡을 것으로 예상되는 예외를 추가하십시오.

+0

죄송합니다. 나는 나의 문제 진술을 정확하게하지 않았다. 다시 말하면 문제는 변수를 구문 분석하는 중입니다. –

+0

@ Yan Check CHEOK : 변수에 유니 코드를 사용하지 않기 때문입니다. ''我们 ''를'u ''我们 ''로 변경하면 사업을 시작하게됩니다. –