python 3.x - msvcrt.getch() returning b'a' instead of 'a'? -


i have following code 1 class:

class _getch:     def __init__(self):         self.impl = _getchwindows()     def read_key(self):          return self.impl()  class _getchwindows:     def __init__(self):         import msvcrt     def __call__(self):         import msvcrt         return msvcrt.getch() 

and have class imported _getch. within other class, tried use read_key provided _getch things in conditional:

r = _getch() key = r.read_key() print(key)  if key = 'a':     #do things elif key = 's':     # other things else:     continue 

when tried input 'a', expecting key 'a', returned b'a' instead. thus, key not fulfill of conditionals, , go continue. why did return b'a'? can make return 'a' instead?

according documentation, msvcrt.getch() returns byte-string.

so need use bytes.decode() method on turn unicode string. hint: if this, should environments encoding , use instead of default utf-8. or can use errors='replace'.

or can change code compare b'a' instead.

n.b.: there syntax error in code; should use == (a comparison operator) in if statement instead of = (assign).


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -