Answer the question
In order to leave comments, you need to log in
How to fix Cyrillic encoding in Python?
The whole Cyrillic alphabet turns into something like this: \xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82
And it is written to the file in the same form.
How can this be fixed?
from chatterbot import ChatBot
# Create a new instance of a ChatBot
bot = ChatBot("Alice",
storage_adapter="chatterbot.adapters.storage.JsonDatabaseAdapter",
logic_adapters=[
"chatterbot.adapters.logic.MathematicalEvaluation",
"chatterbot.adapters.logic.TimeLogicAdapter",
"chatterbot.adapters.logic.ClosestMatchAdapter"
],
input_adapter="chatterbot.adapters.input.TerminalAdapter",
output_adapter="chatterbot.adapters.output.TerminalAdapter",
database="database.db"
)
bot.train(
"Привет",
"Привет)",
"Как дела?",
"Отлично)",
"А у тебя?",
"Хорошо",
)
print("Type something to begin...")
# The following loop will execute each time the user enters input
while True:
try:
# We pass None to this method because the parameter
# is not used by the TerminalAdapter
bot_input = bot.get_response(None)
# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
break
Answer the question
In order to leave comments, you need to log in
Specify the encoding in the header in the first line
What version of python?
If the second - then import immediately after specifying the encoding:
Or explicitly specify:
u"Привет",
u"Привет)",
u"Как дела?",
In [1]: s = b'\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'
In [2]: s
Out[2]: b'\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'
In [3]: print(s)
b'\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'
In [4]: print(s.decode('utf8'))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question