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

前端 未结 3 504
轻奢々
轻奢々 2020-12-31 10:07

I\'m trying to write my first app in Google App Engine with Python (link of the app: http://contractpy.appspot.com/ - it\'s just an experimental app). The entire code is bel

相关标签:
3条回答
  • 2020-12-31 10:30

    You should really use a proper templating system. Jinja2 is included with AppEngine.

    However in the meantime your problem is that your templates are ASCII but your data is not (can't tell if it's utf-8 or unicode). Easy solution is to prefix each template string with u to make it Unicode.

    But, you should really use a proper templating system.

    0 讨论(0)
  • 2020-12-31 10:38

    Here you got solution.

    >>> "t".decode("utf-8")
    u't'
    >>> "\x81".decode("utf-8")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "encodings/utf_8.py", line 7799, in decode
    UnicodeDecodeError: 'utf8' codec can't decode byte 0x81 in position 0: unexpected code byte
    
    >>> "a\x81b".decode("utf-8", "replace")  # this will decode better and the right way.
    u'a\ufffdb'
    
    0 讨论(0)
  • 2020-12-31 10:40

    Set default encoder at the top of your code

    In appcfg.py which resides inside /google/appengine/tools/appcfg.py

    on line 73 add

    import sys
    reload(sys)
    sys.setdefaultencoding("ISO-8859-1")
    
    0 讨论(0)
提交回复
热议问题