[英]How to use .format() on input from .getstr() in Python 3 curses?
我正在嘗試學習Python 3.5中curses庫的一些非常基本的用法。
我面臨以下問題:使用.getstr()
檢索字符串“ foo” .getstr()
,如果我使用字符串方法.format()
,則輸出的是帶有前綴b和撇號的字符串: b'foo'
而不是只是foo
。 .getstr返回的內容是否不同於普通字符串?
這是怎么回事 如何只打印變量?
以下幾行代碼顯示了該問題:
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)
謝謝
從用戶讀取一個字節對象,具有原始行編輯功能。
因此,此API返回的bytes
對象代表了原始數據。 您可以使用a.decode()
將其轉換為文本a.decode()
默認情況下將采用UTF-8編碼)。
文獻:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.