printing dynamically string in one line in python

后端 未结 2 1637
难免孤独
难免孤独 2021-01-03 15:17

I\'m trying to print strings in one line.

I\'ve found solutions but they don\'t works with windows correctly.

I have text file contains names and I want to p

2条回答
  •  [愿得一人]
    2021-01-03 15:40

    Using '\b' as suggested by senderle

    import sys
    import time
    
    sys.stdout.write('name=')
    last_lenght = 0
    with open('names.txt') as names:
        for name in names:
            sys.stdout.write('\b' * last_lenght)    # go back
            sys.stdout.write(' ' * last_lenght)     # clear last name
            sys.stdout.write('\b' * last_lenght)    # reposition
            sys.stdout.write(name.strip())
            sys.stdout.flush()
            last_lenght = len(name.strip())
            time.sleep(0.5)
    

提交回复
热议问题