1

Encoded text I want to read list from file but its getting all coded and .encode doesn't really work

    import json,sys
    with open('your_file.txt') as f:
        lines = f.read().splitlines()

    self.logger.info(lines)
    self.tts.say(lines[1])
exat1as
  • 13
  • 2
  • Can you share your_file.txt file so that I can take a look at it? – Bora Jan 04 '21 at 13:05
  • 0 Разработка поведения робота NAO для использования в процессе обучения 1 Что такое антропоморфный робот? 2 Кибер - педагог Ева На данный момент в России существует кибер-педагог Ева, который был создан в 2016 IT-лицеем КФУ. 3 Робот НАО 4 Конструктор «Хореограф» 5 Функционал робота НАО Робот обладает большим внутренним инструментарием который позволяет создавать и программировать большое количество уникальных функций 6 – exat1as Jan 04 '21 at 13:11

1 Answers1

0

If your file is saved with UTF-8 encoding, this should work:

with open('text.txt', encoding = 'utf-8', mode = 'r') as my_file: 

If this doesn't work, your text file's encoding is not UTF-8. Write your file's encoding in place of utf-8. How to determine the encoding of text?

Or if you share your input file as is, I can figure that out for you.

Bora
  • 1,402
  • 16
  • 19
  • 'encoding' is an invalid keyword argument for this function – exat1as Jan 04 '21 at 13:23
  • Have you imported with `from io import open`? https://docs.python.org/3/library/io.html#text-i-o – Bora Jan 04 '21 at 14:18
  • 1
    it still getting coded info u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f – exat1as Jan 04 '21 at 14:38
  • Ok, that's good. You should print it like this: `yourStringVariableNameHere=u"\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f"` and `print(u"{}".format(yourStringVariableNameHere))`. The output of my code is "Разработка поведения" – Bora Jan 04 '21 at 14:48
  • 1
    in choregraphe print function dont work so i have to use logger.info and i getting TypeError: in method 'info', argument 1 of type 'std::string const &' – exat1as Jan 04 '21 at 14:58
  • Can you try the method proposed here? https://stackoverflow.com/a/10711471/3124100 PS: I'd just log it as unicode, and decode it when I'll use it. – Bora Jan 04 '21 at 15:06