Code

fileopen = open('log.txt', mode="wa") fileopen.write(message.chat.username + ' ' + message.text + '\n') 

I write a bot for Telegram in python 2.7 . message is an object that can output utf-8 characters in Russian , including message.text.username and message.text . How to make the interpreter correctly output everything to a file and transfer it where I have '\ n' to the next line in the file

Mistake

 fileopen.write(message.chat.username + ' ' + message.text + '\n') UnicodeEncodeError: 'ascii' codec can't encode characters in position 8-12: ordinal not in range(128) 
  • It looks like you have unicode and alternate bytes go. This should not be done. Use Unicode for text . Check the types of variables ( type() ). Use io.open() , explicitly specifying the encoding to write Unicode to the file. - jfs
  • Thank you, I'll try to change, if nothing happens, I will ask for help - Raneddo
  • @jfs for some reason, when I open the file, the TypeError problem comes out: 'encoding' As far as I understand, this is the same thing - Raneddo
  • open! = io.open - jfs
  • @jfs I'm just infinitely grateful. It was really very important, and I didn’t find it on any forum, thanks a lot again - Raneddo

0