[英]UnicodeEncodeError: 'ascii' codec can't encode characte
This is my robot code in Python 2.7: 这是我在Python 2.7中的机器人代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot
bot = ChatBot('Test')
Conversa =['Oi','Olá','Tudo bem?','Eu estou bem']
bot.set_trainer(ListTrainer)
bot.train(Conversa)
while True:
quest = input('Voce: ')
resposta = bot.get_response(quest)
print ('Bot: ', resposta)
When I run it gives the following error: 当我运行它会给出以下错误:
Traceback (most recent call last):
File "file/bot.py", line 15, in <module>
quest = input('Voce: ')
File "<string>", line 1, in <module>
NameError: name 'oi' is not defined
If I change the input to raw_input it gives this error too: 如果我将输入更改为raw_input,也会出现此错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 18: ordinal not in range(128)
Is there a reason you're not using Python 3? 您没有使用Python 3是有原因的吗? One of the major reasons for the version was the complete removal of all these weird problems. 该版本的主要原因之一是完全消除了所有这些奇怪的问题。
If you have any string which contains a character that's not in the ASCII codepage, you'll need to use a unicode string, instead of a bytestring. 如果您有任何包含不包含在ASCII代码页中的字符的字符串,则需要使用unicode字符串而不是字节字符串。
In your case, it will look like this: 在您的情况下,它将如下所示:
Conversa =['Oi',u'Olá','Tudo bem?','Eu estou bem']
Though a better solution, if you absolutely positively must start a new project on Python 2, add from __future__ import unicode_literals
at the top of your file. 虽然这是一个更好的解决方案,但如果您绝对肯定必须在Python 2上启动一个新项目, from __future__ import unicode_literals
文件顶部的from __future__ import unicode_literals
添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.