Wrote the following code

conAcc = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\ThirdTask\Northwind.accdb') SqlAccess=conAcc.cursor(); SqlAccess.execute(sql.sql_count_record_clients); CountOfRecords=SqlAccess.fetchone(); conAcc.close(); 

where in the sql.py module there is a line

 sql_count_records_clients='''SELECT COUNT(*) FROM "Бписок ΠΊΠ»ΠΈΠ΅Π½Ρ‚ΠΎΠ²"''' 

As a result, this line in sql.py gives an error

 Traceback (most recent call last): File "D:\ThirdTask\connect.py", line 5, in <module> import json,sqlite3,sql File "D:\ThirdTask\sql.py", line 48 SyntaxError: Non-ASCII character '\xd1' in file D:\ThirdTask\sql.py on line 48, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details 

What needs to be done to make the error disappear?

    2 answers 2

    Python 2.x reads the source code as ascii by default, and if it sees octets greater than 127, it screams SyntaxError .

    To indicate Python in which encoding the file is written, you need to add a special comment to the beginning of the file that matches the regexsp " coding[:=]\s*([-\w.]+) ", As a rule - one of the following types:

     # coding=<ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ°> # -*- coding: <ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ°> -*- # vim: set fileencoding=<ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ°> : 

    Where <ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ°> is actually an encoding, for example, β€œutf-8” or β€œcp1251”.

    This is described in detail in PEP 263 .

      By experience - in such situations the database encoding is usually to blame ...

      • one
        @qnub, but what about the database encoding, if the Python compiler swears at a specific (by me) line in the sql.py module. - ivan89
      • Given that this line receives the response from the database not in the encoding that is expected and trying to read the value instead of the clear characters, it gets '\xd1' . In the expected character encoding with this code does not exist ... - qnub
      • @qnub, and what to do to correct the error? - ivan89
      • Either change the encoding of the database / table / field to UTF-8 or change the connection parameters to the database. More I will not say because with Access did not work. - qnub
      • one
        @qnub, why I questioned your assumption - this is because when you save the sql.py module, a window with the text Non-ASCII found appears, yet not encoding declared. Add your line like # - - coding: cp1251 - - - to your file and your file - ivan89