Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)


See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

I had to add errors argument:

str_text = str(text, "utf-8", errors="ignore")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...