Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to modify/edit the string printed to screen and read it back?

Tags:

python

I'd like to print a string to command line / terminal in Windows and then edit / change the string and read it back. Anyone knows how to do it? Thanks

print "Hell"
Hello!  <---Edit it on the screen
s = raw_input()
print s
Hello!
like image 311
TMS Avatar asked Aug 30 '11 18:08

TMS


1 Answers

You could do some ANSI trickery to make it look like you are editing on screen. Check out this link (also similar to this SO post on colors).

This would only work on certain terminals and configurations. ymmv.

This python script worked in my Cygwin terminal on Win7:

print 'hell'
print '\033[1A\033[4CO!'

Ends up printing hellO! on one line. The 2nd print moves the cursor up one line (Esc[1A) then over 4 characters (Esc[4C]) and then prints the 'O!'.

It wouldn't let you read it back though... only a 1/2 answer.

like image 90
AlG Avatar answered Sep 27 '22 19:09

AlG