Python Forum

Full Version: curses string formats?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone!

I've been trying my hand at a simple curses GUI for a short program. I wanted the program to be able to respond to key presses without the user having to press return, but there were also times when I wanted to have the user type a line of text and hit return, as normally happens with a text console

After trying to wade through a couple basic tutorials and the python documentation, I realized that the following line seemed to work as expected...
st = window.getstr( y, x )
...except that the string was formatted as
b'myString'

I kind of have a intuitive guess that the string is in 'bytecode' but I don't know how reformat this strange string format into something i can use in my program

curses has no problem, and...
window.addstr( y, x, st )
...prints out the string normally:
myString

but the normal python print() statement
print( st )
produces the output:
b'myString'

and if i try
print( str( st ) )
didn't help and the output was:
b'myString'

and finally, trying
print( st[2:-1] )
it did not have the result expected:
b'yStrn'

So maybe this is a python question as much as a curses question:
But how can I convert this string into a "normal everyday python string"?

Thanks in advance!! Smile
This is a bytes object, one of python3's fundamental data types. To transform it into a python string you can use
val = st.decode('utf8')
The converse operation is
st = val.encode('utf8')
By the way, if you want to play with the console, more recent modules exist, such as urwid. They may be more programmer friendly than curses.
Thanks very much! That's enough to get me up and running!!

...and I appreciate the tip about urwid...I definitely got the feeling that curses libraries seemed a little dated so I was wondering if there were some good alternatives out there :-)