简体   繁体   English

Flask-Admin:UnicodeDecodeError:'ascii'编解码器无法解码字节

[英]Flask-Admin: UnicodeDecodeError: 'ascii' codec can't decode byte

I'm trying to build a back-end interface for my application with flask-admin. 我正在尝试使用flask-admin为我的应用程序构建一个后端接口。 When I try to access the form to create a new entry I get: 当我尝试访问表单以创建新条目时,我得到:

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

Going through the stack trace, the problem is that some items in my table contain non-ascii characters. 通过堆栈跟踪,问题是我的表中的某些项目包含非ASCII字符。 How can I solve this issue? 我该如何解决这个问题? Thanks! 谢谢!

您应该使用unicode字符串,就像这样

u"whaterver string"

Just had the same problem with some legacy code. 某些旧代码只是遇到了同样的问题。 This problem happens whenever you: 每当您:

  1. Use Python 2, and 使用Python 2,然后
  2. Represent non-ASCII data via str (rather than unicode ) objects, and 通过str (而不是unicode )对象表示非ASCII数据,并且
  3. Your Python system encoding is ascii (as is mostly the case). 您的Python系统编码是ascii (通常是这样)。

The second issue may stem from having SQLAlchemy String columns where you shold have had Unicode , or from writing 'šömething' when you should have written `u'šömething'' - it may often be quite tricky to pinpoint the actual source of the problem. 第二个问题可能源于您拥有Unicode SQLAlchemy String列,也可能源于您应该编写'šömething'时写的“šömething”-查明问题的实际来源通常很棘手。

It is, however, easy to solve it by hacking the third component of the equation (which is not recommended in general, though). 但是,通过修改方程式的第三部分很容易解决问题(不过一般不建议这样做)。 Adding these lines somewhere in your code should fix things (by tucking the actual problem under the carpet): 将这些行添加到代码中的某处应该可以解决问题(将实际问题隐藏在地毯下):

import sys
reload(sys)
sys.setdefaultencoding('UTF8')

In general, this error is solved by forcing the string array to unicode with the unicode.encode() method . 通常,通过使用unicode.encode()方法将字符串数组强制为unicode可以解决此错误。

From the Python Wiki page on the subject 在主题的Python Wiki页面上

>>> u"a".encode("utf-8")
'a'
>>> u"\u0411".encode("utf-8")
'\xd0\x91'
>>> "a".encode("utf-8")         # Unexpected argument type.
'a'
>>> "\xd0\x91".encode("utf-8")  # Unexpected argument type.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in     range(128)

I suppose this would be best solved by modifying the jinja macro responsible for the field formatting to force the values to unicode. 我想最好通过修改负责字段格式化的jinja宏来强制将值转换为unicode来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnicodeDecodeError:“ ascii”编解码器无法解码字节 - UnicodeDecodeError: 'ascii' codec can't decode byte 如何解决“ UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码字节” - How to solve “UnicodeDecodeError: 'ascii' codec can't decode byte” OpenERP-UnicodeDecodeError:“ ascii”编解码器无法解码字节? - OpenERP - UnicodeDecodeError: 'ascii' codec can't decode byte? UnicodeDecodeError:“ ascii”编解码器无法解码字节0xe4 - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 Python 2.7 UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码字节 - Python 2.7 UnicodeDecodeError: 'ascii' codec can't decode byte UnicodeDecodeError:“ ascii”编解码器无法解码位置的字节0xec - UnicodeDecodeError: 'ascii' codec can't decode byte 0xec in position Python(nltk)-UnicodeDecodeError:“ ascii”编解码器无法解码字节 - Python (nltk) - UnicodeDecodeError: 'ascii' codec can't decode byte UnicodeDecodeError:“ ascii”编解码器无法解码位置4的字节0xdf - UnicodeDecodeError: 'ascii' codec can't decode byte 0xdf in position 4 UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码字节0xc5 - UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 exceptions.UnicodeDecodeError-&#39;ascii&#39;编解码器无法解码字节 - exceptions.UnicodeDecodeError - 'ascii' codec can't decode byte
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM