简体   繁体   中英

Parse multipart/form-data file in UTF-8

I am parsing a multipart/form input with Python's cgi module:

body_file = StringIO.StringIO(self.request.body)
pdict = {'boundary': 'xYzZY'}
httpbody = cgi.parse_multipart(body_file, pdict)
text = self.trim(httpbody['text'])

and I want to print some elements of httpbody that are the UTF-8 encoded. I tried text.decode('utf-8') and unicode(text, encoding='utf-8') , but nothing seems to work. Am I missing something here?

Try the following:

text = self.trim(httpbody['text'])
text.encode('utf-8')

I'm assuming the text variable is in string, if not sure str() . Otherwise, you'll get another error thrown at you.

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