2010-12-21 4 views
2

私は、次のファイルUnicodeEncodeErrorを

dummy.py

#!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形式で保存)dummy.html

hello world 
哈罗世界 
${VARIABLE} 
を持っています

私はからの指示を審問していた

しかし、私はまだ、私は逃したエラー

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にレンダリングしようとしているため動作しません。使用するoutput_encodingを指定する必要があります。

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

そして、裸の例外はありません。キャッチすると予想される例外を追加します。

+0

申し訳ありません。私は問題文を正確にしなかった。私はそれを改言し、問題は変数の解析にあります。 –

+0

@Yan Check CHEOK:あなたの変数にUnicodeを使用していないからです。 ''我们 ''を ''我们 "'に変更し、あなたはビジネスに参加しています。 –

関連する問題