Python Forum
curses string formats?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
curses string formats?
#1
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
Reply
#2
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.
Reply
#3
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 :-)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020