-1

I just need convert entered text(bytes) to string. But if i on cyrillic press Backspace and some character, python throw me this error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 10: invalid continuation byte

What i can do?

# -*- coding: utf-8 -*-
from re import sub
import curses


chatting = True
cols = 0
lines = 0


def get_msg(stdscr):
    str_text = ""
    while chatting:
        inpwin = curses.newwin(2, 30, 1, 0)
        text = inpwin.getstr()
        str_text = str(text, "utf-8") # this string throw error

        stdscr.addstr(0, 0, str_text)
        stdscr.refresh()


def main(stdscr):
    curses.echo()
    stdscr.scrollok(True)
    global cols
    global lines
    lines, cols = stdscr.getmaxyx()
    get_msg(stdscr)


curses.wrapper(main)


GENKY
  • 103
  • 8
  • That *"cyrillic"* hints that your locale may be a problem, e.g., you would have to call `setlocale` in the script to initialize things properly. – Thomas Dickey Nov 09 '19 at 18:06
  • @ThomasDickey no, this does not work. – GENKY Nov 09 '19 at 18:09
  • @ThomasDickey i think, what python's str() does not recognize the this special(and not needed) key combination. But how i can skip this? I tried ```for ch in text:```, but i can not translate this byte to string properly... – GENKY Nov 09 '19 at 18:19

1 Answers1

0
str_text = str(text, "utf-8") 

I had to add errors argument:

str_text = str(text, "utf-8", errors="ignore")
jps
  • 20,041
  • 15
  • 75
  • 79
GENKY
  • 103
  • 8