stdscr.getstr() ignore keys, just string

廉价感情. 提交于 2020-03-06 09:20:46

问题


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)



回答1:


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

I had to change it to

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


来源:https://stackoverflow.com/questions/58781164/stdscr-getstr-ignore-keys-just-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!