简体   繁体   English

ASCII编解码器无法编码字符

[英]Ascii codec can't encode character

I'm running a Flask web service, with text-input, but now I have the problem that the text-input sometimes consists of characters that are not included in the ASCII-character set (Example of error: "(Error: no text provided) 'ascii' codec can't encode character u'\’' in position 20)") 我正在运行带有文本输入的Flask Web服务,但是现在我遇到的问题是,文本输入有时包含不包含在ASCII字符集中的字符(错误示例:“(错误:无文本提供)'ascii'编解码器无法在位置20)中编码字符u'\\ u2019'

My code for the Flask web service looks (somewhat) like this: 我的Flask Web服务代码看起来(有点)像这样:

class Classname(Resource):
    def __init__(self):
       self.reqparse = reqparse.RequestParser()
       self.reqparse.add_argument('text', type=str, required=True, help='Error: no text provided')
       super(Classname,self).__init__()

    def post(self):
       args = self.reqparse.parse_args()
       text = args['text']
       return resultOfSomeFunction(text)

I already tried to turning the ascii-string into unicode, but that didn't work (error: 'unicode' object is not callable). 我已经尝试过将ascii字符串转换为unicode,但这没有用(错误:'unicode'对象不可调用)。 I also tried to add: 我还尝试添加:

text = re.sub(r'[^\x00-\x7f]',r' ',text)

after the rule 在规则之后

text = args['text']

but that also gave me the same error ('ascii' codec can't encode character). 但这也给了我同样的错误(“ ascii”编解码器无法编码字符)。

How can I solve this? 我该如何解决?

Have you tried removing type=str from self.reqparse.add_argument('text', type=str, required=True, help='Error: no text provided') ? 您是否尝试self.reqparse.add_argument('text', type=str, required=True, help='Error: no text provided')删除type=str

Note: 注意:

The default argument type is a unicode string. 默认参数类型是unicode字符串。 This will be str in python3 and unicode in python2 这将是python3中的str和python2中的unicode

Source: http://flask-restful-cn.readthedocs.org/en/0.3.4/reqparse.html 资料来源: http : //flask-restful-cn.readthedocs.org/en/0.3.4/reqparse.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM