Good day!
Help solve the problem with encodings. We have Python 3.2 on which the script is written processing the data received from the form. The data is simply stored in the Postgres database.
Here is a piece of form code:
"form1" action = "/cgi-bin/add_dolz.py" method ="GET"> Наименование: <input type = "text" name = "d_name"><br> Тарифный разряд: <inp ut type = "text" name = "d_razr"> <br><br> <input type = "submit" value = "Добавить"> Here is a piece of the script processing the form: (Python 3.2)
def Main(): # Получаем пераметры скрипта f = cgi.FieldStorage() a = f["d_name"].value b = f["d_razr"].value # Формируем запрос на добавление строки в таблицу quer = "INSERT INTO DOLZ(NAME,RAZR) VALUES ('%s',%s)" % (a,b) # Подсоединяемся к серверу conn = psycopg2.connect(host=HOST, database=DBASE,user=USER,password=PASS) cur = conn.cursor() # Выполняем запрос cur.execute(quer) # Сохраняем результаты запроса в БД conn.commit() # Закрываем соединения с БД cur.close() conn.close() # Формируем ответную html страницу thepage = '''<html> <head> <title>Сообщение</title> </head> <body>Результаты сохранены в БД PostgreSQL</body> </html>''' # отправляем страницу на сервер PrintPage(thepage) # ******************************************************** if __name__ == '__main__': Main() When I enter the data in the form in the English layout - everything works fine. Data is stored in the database.
When I enter the fields in Cyrillic - the following error occurs in the browser:
Traceback (most recent call last): File "c: shttpswwwcgi-binadd_dolz.py", line 68, in Main () File "c: shttpswwwcgi-binadd_dolz.py", line 45, in Main cur.execute (quer) File " C: Program FilesPython 3.2.1libencodingscp1251.py ", line 12, in encode return codecs.charmap_encode (input, errors, encoding_table) UnicodeEncodeError: 'charmap' codec can not be encoded characters in position 37-41: character maps to
I understand that the data from the form comes in cp1251 encoding, they must somehow be converted to unicode, before being written to the database ??
Thank you in advance.