stdscr.getstr() 忽略键,只是字符串
stdscr.getstr() ignore keys, just string
我只需要将输入的文本(字节)转换为字符串。
但是如果我在西里尔文中按 Backspace 和一些字符,python 会抛出这个错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 10: invalid continuation byte
我能做什么?
# -*- 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)
str_text = str(text, "utf-8")
我必须添加 errors
参数:
str_text = str(text, "utf-8", errors="ignore")
我只需要将输入的文本(字节)转换为字符串。 但是如果我在西里尔文中按 Backspace 和一些字符,python 会抛出这个错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 10: invalid continuation byte
我能做什么?
# -*- 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)
str_text = str(text, "utf-8")
我必须添加 errors
参数:
str_text = str(text, "utf-8", errors="ignore")