简体   繁体   中英

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

I'm trying to learn some very basic use of the curses library in Python 3.5.

I'm facing the following problem: after retrieving the string "foo" with .getstr() , if I use the string method .format() what is printed is the string with a prefixed b and apostrophes: b'foo' instead of just foo . Does .getstr return something different than a common string?

What is happening here? What should I do to have just the variable printed?

Here few lines of code to show the issue:

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)

Thank you

From getstr docs :

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

So this API returns a bytes object that represents, well, raw data. You can convert it to text using a.decode() (it will assume UTF-8 encoding by default).

Literature:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM