Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\xa0';位置4:序号不在范围内(128)_Python_Django_Unicode_Python 2.x - Fatal编程技术网

Python UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\xa0';位置4:序号不在范围内(128)

Python UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\xa0';位置4:序号不在范围内(128),python,django,unicode,python-2.x,Python,Django,Unicode,Python 2.x,我在Unicode方面遇到了一些问题。我是一名编程初学者,因此我无法理解与此问题相关的其他答案 回溯 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

我在Unicode方面遇到了一些问题。我是一名编程初学者,因此我无法理解与此问题相关的其他答案

回溯

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)
有问题的代码部分

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
请告诉我是否缺少部分代码或部分回溯


有人能帮帮我吗?

问题在
str(get\u one\u phrasalverb)
里面。错误表明,无法使用
ascii
编码将
get\u one\u phrasalverb
转换为
str


因此,您首先需要知道
get\u one\u phrasalverb
的编码,如果编码是
utf8
,您可以使用
get\u one\u phrasalverb.encode('utf8')
而不是
str(get\u one\u phrasalverb)
,谢谢您的回答。现在我得到:对象没有属性“encode”。你知道它可能是什么吗?什么是
get\u-one\u phrasalverb
,你可以用
type(get\u-one\u phrasalverb)
打印它,因为它的类型不是unicode,你需要在
phrasalverb
\uu-str\uuuuu
方法中进行编码如果
self.name
是字符串,你可以这样做