When I use Russian letters the code does not work:

average = int(re.findall(u'Среднее = (\d+)', out)[0]) 

Exception: SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xd1 in position 0: invalid continuation byte

  • Well, out in what encoding? - mkkik
  • Is there a source code declaration encoding at the top? Does the actual file encoding match this declaration? Give a complete (but minimal) sample code and a full traceback. What version of Python, the operating system, where is the output going (console, IDE)? - jfs
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Probably SyntaxError caused by the fact that the file encoding declaration (at the top of the file itself) does not match the actual file encoding. Use an editor that saves files in utf-8.

For example, if you save the text in the utf8-charset.py file:

 #!/usr/bin/env python # -*- coding: utf-8 -*- s = u"по-русски" 

and run: python utf8-charset.py , then nothing will happen (no error). But if this file is saved in a different (inconsistent declaration inside the file) encoding:

 $ iconv -t 866 utf8-charset.py > wrong-866-charset.py 

then when you run the same command: python wrong-866-charset.py error is printed:

  File "wrong-866-charset.py", line 3 SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xaf in position 0: invalid start byte 

    Add the definition of the source code encoding in the source file; set the encoding to UTF-8

     #!/usr/bin/env python # -*- coding: utf8 -*- 

    By default, the source code is interpreted in ASCII encoding . ASCII encoding does not have Russian characters.
    Additionally, you can also check and, if necessary, re-save the source file in UTF-8 encoding in any text editor. The problem should disappear.

    • error already mentioned utf-8. Therefore, ascii has nothing to do with it. The most likely reason is that the actual file encoding is not utf-8 (which is already listed in the source code) - jfs
    • @jsf, yes, you are right. Then it remains to re-save the original file in utf-8 encoding. - Alexcei Shmakov