0

I use end="\r" to print count in on the same line. My python is too fast and CR has not the time to apply. The script goes to next line...

for i in range(10000):
    print(i,end="\r")

What it outputs:

3495

6735

9994

9999

It must outputs dynamically the number of lines, on the same line. Then 2 seconds later the outputs must be 10000 in this example...

I have no choice to add a sleep(0.01) but it slows down a lot my script. I have 8Go RAM and 2 CPU. So the script is very fast and the CR might not be taken into account ???

EDIT: answer is to flush. "Printing on the same line on a jupyter notebook" solution is not working because I don't want the sleep function which will the efficiency of the script (I have 400k to 28M items)

  • Works for me. Are you sure that this isn’t a problem with your console? – poke Jun 27 '19 at 08:58
  • I have 8Go RAM and 2 CPU. So the script is very fast and the CR might not be taken into account ??? –  Jun 27 '19 at 09:01
  • 4
    I don’t see what the CPU has to do with this. How a carriage return character is applied is something that is up to the console rendering the output. You could try a `sys.stdout.flush()` after the print. – poke Jun 27 '19 at 09:01
  • @TTeaTie what is your expected output? – Kushan Gunasekera Jun 27 '19 at 09:05
  • 2
    [This is what it looks like for me](https://i.stack.imgur.com/E6KcC.gif). – poke Jun 27 '19 at 09:14
  • @poke yes this is what I need. But I use jupyter notebook on a ZOE analitycs zapp –  Jun 27 '19 at 09:19
  • If you search on Google for “carriage return jupyter notebook”, you will see lots of discussions on that topic. It seems that it is not fully supported since *“[Jupyter Notebook] is not a terminal emulator”*. You can use other means of output (e.g. a JavaScript progress bar) instead. – poke Jun 27 '19 at 09:25
  • 1
    Check [this](https://stackoverflow.com/a/46649835/8353711) – shaik moeed Jun 27 '19 at 09:27
  • @poke thanks you solved my issue with flushing. I hope it will not take too many resouces (400k lines so 400k flushes) –  Jun 27 '19 at 10:10

1 Answers1

1

Try this,

for i in range(10000):
    print("\r {}".format(i),end=" ")
shaik moeed
  • 5,300
  • 1
  • 18
  • 54