简体   繁体   中英

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 4: ordinal not in range(128)

I'm facing some trouble with Unicode. I'm a beginner in programming, so I couldn't understand the other answers related to this issue here.

TraceBack

Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/django/django_project/learn/views.py" in index
  19.         if str(get_one_phrasalverb) not in cannot_be_random:

Exception Type: UnicodeEncodeError at /learn/keep-from/
Exception Value: 'ascii' codec can't encode character u'\xa0' in position 4: ordinal not in range(128)

Problematic code part

cannot_be_random = request.session.get('cannot_be_random')

if str(get_one_phrasalverb) not in cannot_be_random:
     cannot_be_random.append(str(get_one_phrasalverb))
     request.session['cannot_be_random'] = cannot_be_random

Please tell me if some part of the code or part of the traceback is missing.

Could someone help me, please?

The issue is inside str(get_one_phrasalverb) . The error says that get_one_phrasalverb could not be converted to str with ascii encoding.

So you need to know the encoding of get_one_phrasalverb first, if the encoding is utf8 , you could use get_one_phrasalverb.encode('utf8') instead of str(get_one_phrasalverb)

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