0

I have problem in appengine with the à è ì ò ù I know that the problem is solved adding the u like u"something", but this not working with the ''' something on many lines '''. How to solve it?

The other problem I have is when I try to store vars with this kind of characters in it then I have an error when I try to display them. 'ascii' codec can't encode character u'\xe8' in position 5: ordinal not in range(128)

Thanks for help. (I searched for documentation, but I can't find nothing, if you have any link please push me in the right direction).

Grostein
  • 33
  • 1
  • 5

1 Answers1

0

you could use a function like this:

def to_unicode(s):
    """force conversion to unicode"""
    if not isinstance(s, basestring):
        s = str(s)
    if isinstance(s, unicode):
        return s
    else:
        return s.decode('utf-8', 'ignore')

and before you put the value pass it to this function

entity.text = to_unicode('üöä')
entity.put()

this link might help you understand the topic:
http://blog.notdot.net/2010/07/Getting-unicode-right-in-Python

edit: this other question made me understand the topic a little better
Python and UTF-8: kind of confusing

Community
  • 1
  • 1
aschmid00
  • 7,038
  • 2
  • 47
  • 66
  • Sorry, but that's a really nasty solution. Simply using `x.decode('utf-8')` on a byte string (assuming it _is_ in UTF-8) is a better solution; not having byte string literals in the first place is a better idea. – Nick Johnson May 25 '12 at 05:54
  • what exactly do you mean with `not having byte string literals in the first place`? – aschmid00 May 25 '12 at 13:15
  • Quote Aschmid. What you mean? – Grostein May 25 '12 at 14:56
  • is decode a python class? Do I need to import it? The best use is: var = '''something in many lines with the è ''' / self.response.out.write(var) or is better self.response.out.write(''' many lines ''').decode('utf-8') – Grostein May 25 '12 at 14:58
  • I mean that if you're trying to enter a literal string in Python, you should be prefixing it with `u` - not using a raw string, then decoding it at runtime. – Nick Johnson May 26 '12 at 02:35
  • @NickJohnson i was just checking out the appengine boilerplate and found this decode function: https://github.com/metachris/appengine-boilerplate/blob/master/app/tools/common.py#L18 is this the correct way to do it? – aschmid00 Aug 17 '12 at 12:49
  • I suppose, but it'd be better to be unambiguous about what data type you're dealing with in the first place, and just use the built-in methods. – Nick Johnson Aug 22 '12 at 10:05