如何在 Python 3 curses 中对来自 .getstr() 的输入使用 .format()?

How to use .format() on input from .getstr() in Python 3 curses?

我正在尝试学习 Python 3.5 中 curses 库的一些非常基本的用法。

我面临以下问题:在使用 .getstr() 检索字符串 "foo" 后,如果我使用字符串方法 .format(),则打印的是带有前缀 b 的字符串和撇号:b'foo' 而不仅仅是 foo。 .getstr return 与普通字符串有什么不同吗?

这里发生了什么?我应该怎么做才能只打印变量?

这里用几行代码来说明问题:

import curses


def main(scr):
    scr.clear()
    curses.echo()
    scr.addstr(0, 0, "Write...")
    a = scr.getstr(1, 0)
    scr.addstr(
        2, 0, "You wrote...\nWith string.format:"
        "\n{}\nCalling directly the variable:\n".format(a))
    scr.addstr(6, 0, a)
    scr.addstr(8, 0, "Press Return to quit")
    scr.getkey()

curses.wrapper(main)

谢谢

From getstr docs :

Read a bytes object from the user, with primitive line editing capacity.

所以这个 API returns 一个 bytes 对象,表示原始数据。您可以使用 a.decode() 将其转换为文本(默认情况下它将采用 UTF-8 编码)。

文学: