在python的一行中动态打印字符串 [英] printing dynamically string in one line in python
本文介绍了在python的一行中动态打印字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在一行中打印字符串.
I'm trying to print strings in one line.
我找到了解决方案,但是它们不能在Windows上正常工作.
I've found solutions but they don't works with windows correctly.
我的文本文件包含名称,我想这样打印它们
I have text file contains names and I want to print them like this
name=john
,然后将john更改为下一个名字,并保留name=
,我编写了此代码,但在Windows上无法正常工作:
name=john
then change john to next name and keep name=
, I've made this code but didn't work correctly with windows:
op = open('names.txt','r')
print 'name=',
for i in op.readlines():
print '\r'+i.strip('\n')
感谢您的时间
推荐答案
使用senderle建议的'\ b'
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)
这篇关于在python的一行中动态打印字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文