SyntaxError: Non-ASCII character '\xd1'
I 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 module sql.py there is a string
sql_count_records_clients='''SELECT COUNT(*) FROM "Список клиентов"'''
As a result, on this line in sql.py returns the 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 should I do to make the error disappear?
3
2 answers
Python 2. x reads the source code as ascii by default, and if it sees octets greater than 127, it shouts this very SyntaxError
.
To specify the Python encoding in which the file is written, you need to add a special comment at the beginning of the file, suitable for regexp "coding[:=]\s*([-\w.]+)
", usually one of the following types:
# coding=<кодировка>
# -*- coding: <кодировка> -*-
# vim: set fileencoding=<кодировка> :
Where <кодировка>
is the actual encoding, for example, "utf-8" or "cp1251".
This is described in detail in PEP 263.
5
Author: drdaeman, 2012-11-03 16:13:39
In my experience, in such situations, the DB encoding is usually to blame...
-1
Author: qnub, 2012-11-03 14:35:04