简体   繁体   中英

UnicodeEncodeError: 'ascii' codec can't encode characte

This is my robot code in 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:

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

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.

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