Print in one line dynamically

后端 未结 20 3214
梦谈多话
梦谈多话 2020-11-21 23:32

I would like to make several statements that give standard output without seeing newlines in between statements.

Specifically, suppose I have:

for it         


        
相关标签:
20条回答
  • 2020-11-21 23:41

    I think a simple join should work:

    nl = []
    for x in range(1,10):nl.append(str(x))
    print ' '.join(nl)
    
    0 讨论(0)
  • 2020-11-21 23:41

    "By the way...... How to refresh it every time so it print mi in one place just change the number."

    It's really tricky topic. What zack suggested ( outputting console control codes ) is one way to achieve that.

    You can use (n)curses, but that works mainly on *nixes.

    On Windows (and here goes interesting part) which is rarely mentioned (I can't understand why) you can use Python bindings to WinAPI (http://sourceforge.net/projects/pywin32/ also with ActivePython by default) - it's not that hard and works well. Here's a small example:

    import win32console, time
    
    output_handle = win32console.GetStdHandle(  win32console.STD_OUTPUT_HANDLE )
    info = output_handle.GetConsoleScreenBufferInfo()
    pos = info["CursorPosition"]
    
    for i in "\\|/-\\|/-":
        output_handle.WriteConsoleOutputCharacter( i, pos )
        time.sleep( 1 )
    

    Or, if you want to use print (statement or function, no difference):

    import win32console, time
    
    output_handle = win32console.GetStdHandle(  win32console.STD_OUTPUT_HANDLE )
    info = output_handle.GetConsoleScreenBufferInfo()
    pos = info["CursorPosition"]
    
    for i in "\\|/-\\|/-":
        print i
        output_handle.SetConsoleCursorPosition( pos )
        time.sleep( 1 )
    

    win32console module enables you to do many more interesting things with windows console... I'm not a big fan of WinAPI, but recently I realized that at least half of my antipathy towards it was caused by writing WinAPI code in C - pythonic bindings are much easier to use.

    All other answers are great and pythonic, of course, but... What if I wanted to print on previous line? Or write multiline text, than clear it and write the same lines again? My solution makes that possible.

    0 讨论(0)
  • 2020-11-21 23:42

    Or even simpler:

    import time
    a = 0
    while True:
        print (a, end="\r")
        a += 1
        time.sleep(0.1)
    

    end="\r" will overwrite from the beginning [0:] of the first print.

    0 讨论(0)
  • 2020-11-21 23:43

    So many complicated answers. If you have python 3, simply put \r at the start of the print, and add end='', flush=True to it:

    import time
    
    for i in range(10):
        print(f'\r{i} foo bar', end='', flush=True)
        time.sleep(0.5)
    

    This will write 0 foo bar, then 1 foo bar etc, in-place.

    0 讨论(0)
  • 2020-11-21 23:46

    Use print item, to make the print statement omit the newline.

    In Python 3, it's print(item, end=" ").

    If you want every number to display in the same place, use for example (Python 2.7):

    to = 20
    digits = len(str(to - 1))
    delete = "\b" * (digits + 1)
    for i in range(to):
        print "{0}{1:{2}}".format(delete, i, digits),
    

    In Python 3, it's a bit more complicated; here you need to flush sys.stdout or it won't print anything until after the loop has finished:

    import sys
    to = 20
    digits = len(str(to - 1))
    delete = "\b" * (digits)
    for i in range(to):
       print("{0}{1:{2}}".format(delete, i, digits), end="")
       sys.stdout.flush()
    
    0 讨论(0)
  • 2020-11-21 23:47
    for item in range(1,100):
        if item==99:
            print(item,end='')
        else:
            print (item,end=',')
    

    Output: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99

    0 讨论(0)
提交回复
热议问题