defunkt / pystache

Mustache in Python
http://mustache.github.com/
MIT License
1.31k stars 308 forks source link

pystache.render() with special characters #168

Closed eddpascoal closed 10 years ago

eddpascoal commented 10 years ago

i'm getting a very annoying problem with pystache using special characters.. :(

How can i render the follow example: import pystache new_render = pystache.Renderer() example = new_render.render("person:{{name}}", {"name":"João"})

Anyone have any idea how to lead with special characters using pystache? when i try to run this 'example' it occurs me a UnicodeDecodeError:

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

cjerdonek commented 10 years ago

Can you copy the entire Python file to a gist so we can see it?

cjerdonek commented 10 years ago

Also, are you using Python 2 or 3?

eddpascoal commented 10 years ago

Hi cjerdonek yes im using Python 2.7 in windows with netbeans IDE 7.4

python example is pretty much what i'm showing in example as you will find in https://gist.github.com/MrZogs/6a31e9daf110ea84a414

cjerdonek commented 10 years ago

I would recommend reading up on Unicode and Python, etc. This is partly an issue with familiarizing yourself with how Python handles such strings.

Try this in your code (also note the declaration for Python of a source code encoding):

# encoding: utf8

import pystache
weird_json ={"person": "João"}
new_render = pystache.Renderer(string_encoding='utf8')
print new_render.render("Hi {{person}}!", weird_json)

This worked for me.

Also, even better would be to use Unicode strings as your input instead of byte strings:

# encoding: utf8

import pystache
weird_json ={"person": u"João"}
new_render = pystache.Renderer()
print new_render.render("Hi {{person}}!", weird_json)

This also worked for me.

eddpascoal commented 10 years ago

Right, it works! I read a few things about unicode. I try to run that in Netbeans IDE and it occurs me: "UnicodeEncodeError: 'ascii' codec can't encode character u'\xe3' in position 5: ordinal not in range(128)" but when i run it on the cmd.exe with python it works fine. I think it is an IDE misconfiguration issue.

Thanks a lot for your help.

cjerdonek commented 10 years ago

Okay, cool. Glad to be of help.

eddpascoal commented 10 years ago

Just to help others with the same problem about using a Netbeans IDE for python. I found a Netbeans bug comment about python encoding that may be help. See in: https://netbeans.org/bugzilla/show_bug.cgi?id=171688 . For this reason i may change my IDE to work with.