User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2876
Joined: Sat Mar 03, 2012 11:57 am
Location: UK

How to print repeatedly to the same line?

Sat Oct 27, 2012 8:43 pm

I've got a program that I need to operate in the command line.
Basically it goes through a loop and prints a character for each iteration.
I'd like them all to be following on from each other on the same line like this...

Code: Select all

+++++++++------------
instead of this

Code: Select all

+
+
-
-
I tried appending a comma , but then it fails to print anything at all until the loop has gone through all iterations. I want it to display each character as it goes.

I'm sure it must be simple. But it eludes me.

Anyone know how to achieve this?
Alex Eames RasPi.TV, RasP.iO

User avatar
malakai
Posts: 1382
Joined: Sat Sep 15, 2012 10:35 am

Re: How to print repeatedly to the same line?

Sat Oct 27, 2012 8:51 pm

Not sure what your using but you search for adding characters to string.

so

h=null
inputh=null

ask for input
inputh = what was input by user
h=h+inputh

h was null so it is just whatever iputh is set to

user puts in a

now h=a

user puts in b

now h=ab you could have it add a space if h not null add h+space+inputh

print h

does that make sense? I would test out a script but python, c, etc..
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: How to print repeatedly to the same line?

Sat Oct 27, 2012 9:08 pm

Do you have any sample code to demonstrate the printing??

Perhaps this might help - http://stackoverflow.com/questions/4933 ... e-or-space

Richard S.

yeahbox
Posts: 36
Joined: Sun May 27, 2012 10:35 pm
Location: Manchester, UK

Re: How to print repeatedly to the same line?

Sat Oct 27, 2012 9:41 pm

alexeames wrote:I tried appending a comma , but then it fails to print anything at all until the loop has gone through all iterations. I want it to display each character as it goes.

I'm sure it must be simple. But it eludes me.
Everything is simple when you know the answer :) You need to flush stdout in order to see your "ticks". If you run the same code in WinXP you will see what you expect but in Linux you need to call sys.stdout.flush()

Code: Select all

import sys, time

for i in (1, 2, 3, 4):
    print "*",
    sys.stdout.flush()
    time.sleep(0.5)    
print "Done"

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2876
Joined: Sat Mar 03, 2012 11:57 am
Location: UK

Re: How to print repeatedly to the same line?

Sat Oct 27, 2012 10:20 pm

Thanks yeahbox. That's great. How can I get rid of the space between each entry?
That gives me...

* * * * Done
I would like
****Done

I tried escaping with a backspace and soon got in a mess. :)

Also tried something from Redhawk's link (thanks Redhawk :) )

Code: Select all

sys.stdout.write('*')
Which does leave out the spaces, but bombs out when it hits the end of the line. I'd like it to wrap. My script needs it to wrap over several lines.

This is pretty much the final hurdle in a little project. When it's done I will let it loose on the world. :lol:
Alex Eames RasPi.TV, RasP.iO

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2876
Joined: Sat Mar 03, 2012 11:57 am
Location: UK

Re: How to print repeatedly to the same line?

Sat Oct 27, 2012 10:25 pm

malakai wrote:Not sure what your using but you search for adding characters to string.
---SNIP---
I would test out a script but python, c, etc..
Thanks for trying to help. I thought it would be clear I was using Python because I posted in the Python forum. :D

But you're right - I should have specified Python 2.7 :lol:
Alex Eames RasPi.TV, RasP.iO

yeahbox
Posts: 36
Joined: Sun May 27, 2012 10:35 pm
Location: Manchester, UK

Re: How to print repeatedly to the same line?

Sat Oct 27, 2012 11:00 pm

alexeames wrote:How can I get rid of the space between each entry?
Ah, enter some python magic :) You can use the print() function from python 3.x even though you are running 2.7. See http://docs.python.org/library/functions.html#print for the official doc. Note that the "from __future__" stuff must be at the top in your module and that you can't mix and match "print" and "print()" in the same module

Code: Select all

from __future__ import print_function

import sys, time

for i in (1, 2, 3, 4):
    print("*", sep='', end='')
    sys.stdout.flush()
    time.sleep(0.5)
print("Done")

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2876
Joined: Sat Mar 03, 2012 11:57 am
Location: UK

Re: How to print repeatedly to the same line?

Sun Oct 28, 2012 9:13 am

Awesome. Thank you so much for this. :)

So that means all my print statements have to be in Python 3 format in this program? That shouldn't be too much bother. :D
Alex Eames RasPi.TV, RasP.iO

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2876
Joined: Sat Mar 03, 2012 11:57 am
Location: UK

Re: How to print repeatedly to the same line?

Sun Oct 28, 2012 1:37 pm

This is working really well. Just one last thing about it though. It stalls when it gets to the end of the line. I need it to wrap over to the next line. What's the best way of doing that?

I imagine I could find a way of trying to get the number of columns on the screen and use the loop to count how many times it's printed, then stick in a \n at the right time...

...but I'm willing to bet there's another Python Magic way of doing it more efficiently. :D
Alex Eames RasPi.TV, RasP.iO

yeahbox
Posts: 36
Joined: Sun May 27, 2012 10:35 pm
Location: Manchester, UK

Re: How to print repeatedly to the same line?

Sun Oct 28, 2012 1:57 pm

I did some quick googling and found this, http://stackoverflow.com/questions/5667 ... -in-python

So my slight modification of that would be

Code: Select all

import os
rows, columns = map(int, os.popen('stty size', 'r').read().split())
I don't think this is a general solution that works everywhere but it might be sufficient for your needs.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2876
Joined: Sat Mar 03, 2012 11:57 am
Location: UK

Re: How to print repeatedly to the same line?

Fri Nov 02, 2012 1:56 pm

Thanks all for help. Sorry for being a bit elusive, but I can now show you what I wanted this for...
http://www.youtube.com/watch?v=qp8UCfITSAY&feature=plcp

I've been trying to port some of the Gertboard software to Python. The printing on one line was needed for the screen output on the motor program. :D

I've got it working not quite exactly as the original, but close enough that I don't think it matters. :D
Alex Eames RasPi.TV, RasP.iO

Return to “Python”