1

Can't solve typical issue with encodings. Cyrrlic text is received via post and error is raised

'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

The text key itself has a look and it must cyrillic text in russian: u'\u043f\u0440\u043e'

After that error tried this way and some others:

  key = key.decode('ascii').encode('utf8')

or :

key = key.decode('ascii')

Localy it works, error is raised in production only. Python system encoding in production is utf8

EDIT: in order to clear things up. Error is raised on form handler function(again, works localy, doesn't in production)

def search(request):
     if request.method == 'POST':
        key = request.POST.get("key")
        if key is not None:
        ..

So it's str received from input form, and error is first raised at this point, so I supposed it must be decoded but it didn't help.

more traceback:

UnicodeEncodeError at /search/

'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

  • 1
    You would be encoding from unicode `.encode("utf-8")`, I don't see how decode would work anywhere as str objects have no decode in python3, pretty sure you are using python2. – Padraic Cunningham Apr 17 '17 at 14:16
  • @PadraicCunningham now I see that you are probably right. Just there is system wide installed python2 in vps, and virtualenv is python3, so I was sure it was using python3, as far as I opened python shell always after activating 'source venv/bin/activate' and 'python manage.py shell' and there python3 is selected –  Apr 17 '17 at 14:26
  • @PadraicCunningham actually now it's ''str' object has no attribute 'decode'' so yeah it's python 3 –  Apr 17 '17 at 14:29
  • well, I would forget the decode anyway, you have a str already, why are you trying to decode, do you sometimes get bytes? – Padraic Cunningham Apr 17 '17 at 14:42
  • @PadraicCunningham well, I added decoding because I was sure it was an issue. Wothout any decoding error is the sams –  Apr 17 '17 at 14:43
  • 1
    How exactly are you getting the error, what is the actual traceback and what exactly are you doing with the str? – Padraic Cunningham Apr 17 '17 at 14:48
  • @PadraicCunningham it's str from input form, error is first raised on checkpoint whether it's None or not(but no matter). or if decoding before it's raised on decoding line –  Apr 17 '17 at 15:01
  • The locale is set to utf-8 on the server? – Padraic Cunningham Apr 17 '17 at 15:03
  • @PadraicCunningham yeah, LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 LC_ALL=en_US.UTF-8 –  Apr 17 '17 at 15:04
  • 1
    I do remember something before where the locale settings were different for the server process, I am trying to find the old question/answer here. – Padraic Cunningham Apr 17 '17 at 15:20
  • May I see `type(key)` after `... request (`. – stovfl Apr 17 '17 at 16:18
  • @stovfl class str –  Apr 17 '17 at 16:27
  • Read this, http://stackoverflow.com/questions/42825027/openpyxl-convert-cell-value-from-utf-8-to-ascii – stovfl Apr 17 '17 at 16:55

0 Answers0