简体   繁体   中英

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)")

My code for the Flask web service looks (somewhat) like this:

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). 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).

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') ?

Note:

The default argument type is a unicode string. This will be str in python3 and unicode in python2

Source: http://flask-restful-cn.readthedocs.org/en/0.3.4/reqparse.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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