Print in one line dynamically

后端 未结 20 3217
梦谈多话
梦谈多话 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:58

    A comma at the end of the print statement omits the new line.

    for i in xrange(1,100):
      print i,
    

    but this does not overwrite.

    0 讨论(0)
  • 2020-11-22 00:00

    The best way to accomplish this is to use the \r character

    Just try the below code:

    import time
    for n in range(500):
      print(n, end='\r')
      time.sleep(0.01)
    print()  # start new line so most recently printed number stays
    
    0 讨论(0)
  • 2020-11-22 00:02

    change

    print item
    

    to

    print "\033[K", item, "\r",
    sys.stdout.flush()
    
    • "\033[K" clears to the end of the line
    • the \r, returns to the beginning of the line
    • the flush statement makes sure it shows up immediately so you get real-time output.
    0 讨论(0)
  • 2020-11-22 00:02

    If you just want to print the numbers, you can avoid the loop.

    # python 3
    import time
    
    startnumber = 1
    endnumber = 100
    
    # solution A without a for loop
    start_time = time.clock()
    m = map(str, range(startnumber, endnumber + 1))
    print(' '.join(m))
    end_time = time.clock()
    timetaken = (end_time - start_time) * 1000
    print('took {0}ms\n'.format(timetaken))
    
    # solution B: with a for loop
    start_time = time.clock()
    for i in range(startnumber, endnumber + 1):
        print(i, end=' ')
    end_time = time.clock()
    timetaken = (end_time - start_time) * 1000
    print('\ntook {0}ms\n'.format(timetaken))
    

    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 100 took 21.1986929975ms

    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 100 took 491.466823551ms

    0 讨论(0)
  • 2020-11-22 00:02

    In Python 3 you can do it this way:

    for item in range(1,10):
        print(item, end =" ")
    

    Outputs:

    1 2 3 4 5 6 7 8 9 
    

    Tuple: You can do the same thing with a tuple:

    tup = (1,2,3,4,5)
    
    for n in tup:
        print(n, end = " - ")
    

    Outputs:

    1 - 2 - 3 - 4 - 5 - 
    

    Another example:

    list_of_tuples = [(1,2),('A','B'), (3,4), ('Cat', 'Dog')]
    for item in list_of_tuples:
        print(item)
    

    Outputs:

    (1, 2)
    ('A', 'B')
    (3, 4)
    ('Cat', 'Dog')
    

    You can even unpack your tuple like this:

    list_of_tuples = [(1,2),('A','B'), (3,4), ('Cat', 'Dog')]
    
    # Tuple unpacking so that you can deal with elements inside of the tuple individually
    for (item1, item2) in list_of_tuples:
        print(item1, item2)   
    

    Outputs:

    1 2
    A B
    3 4
    Cat Dog
    

    another variation:

    list_of_tuples = [(1,2),('A','B'), (3,4), ('Cat', 'Dog')]
    for (item1, item2) in list_of_tuples:
        print(item1)
        print(item2)
        print('\n')
    

    Outputs:

    1
    2
    
    
    A
    B
    
    
    3
    4
    
    
    Cat
    Dog
    
    0 讨论(0)
  • 2020-11-22 00:03

    Like the other examples,
    I use a similar approach but instead of spending time calculating out the last output length, etc,

    I simply use ANSI code escapes to move back to the beginning of the line and then clear that entire line before printing my current status output.

    import sys
    
    class Printer():
        """Print things to stdout on one line dynamically"""
        def __init__(self,data):
            sys.stdout.write("\r\x1b[K"+data.__str__())
            sys.stdout.flush()
    

    To use in your iteration loop you would just call something like:

    x = 1
    for f in fileList:
        ProcessFile(f)
        output = "File number %d completed." % x
        Printer(output)
        x += 1   
    

    See more here

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