Hello. Trying to implement a function in PL / Python (PostgreSQL 9.5, Python 2.7). So I just can not work together with Cyrillic functions. Below is the actual code:

create or replace function pytest() returns varchar as $$ import os, pymorphy2 morph = pymorphy2.MorphAnalyzer() word = morph.parse(u'смартфоны')[0]; #return word.word # Возвращает ñð¼ð°ñññð¾ð½ñ #return u'смартфоны' # Возвращает ÑмаÑÑÑÐ¾Ð½Ñ return 'смартфоны' # Возвращает смартфоны $$ language plpython2u; 

At the same time, in repr (). Decode ('unicode_escape') was wrapped, and manually encoded / decoded. Anyway, kryakozyabry, the truth is different. Moreover, if you drive a word in Cyrillic into a variable, then all the rules are displayed. Can anyone come across? The database I have is in UTF8 (en_US.UTF-8). What could be the problem, I'll never know. Help me please.

  • Who advised you to sys.setdefaultencoding('utf-8') ? - jfs
  • I was looking for google. On stackoverflow and advised. Even if you remove it, it still kryakozyabry. - illatif
  • You can give a specific link. If I thought it would solve your problem, I would write as an answer, not a comment. First you have to isolate the problem - you have too many parts: 1- does return u'смартфоны' ? If it works, fix pymorphy2 (outside pl / python). If it doesn’t work, remove pymorphy2 from the example. 2- naturally remove setdefaultencoding. This design can hide problems. 3- do not use repr().decode('unicode_escape') . The crawlers indicate that the text has been recoded using incompatible encoding. - jfs
  • @jfs about repr () was advised to me here python.su/forum/topic/31533 Regarding setdefaultencoding - even if you remove the function, it’s still a hog. Apparently the problems are inside pymorphy2, because if just return 'smartphones', then everything returns normally. - illatif
  • again: setdefaultencoding call should be removed regardless of your current problem. Although its absence can help in debugging both this and other problems with the text (and vice versa: its presence can hide other possible problems - do you not think that you have only one bug in the program?). - jfs

1 answer 1

This is how the script worked both in PL / Python and outside PL / Python.

 create or replace function pytest(p_word varchar) returns varchar as $$ import os, pymorphy2 morph = pymorphy2.MorphAnalyzer() word = morph.parse(p_word.decode('utf-8'))[0]; return word.normal_form #word.inflect({'gent'}) $$ language plpython2u;