我想做几个语句,给出标准输出,而不会在语句之间看到换行符.
具体来说,假设我有:
for item in range(1,100):
print item
Run Code Online (Sandbox Code Playgroud)
结果是:
1
2
3
4
.
.
.
Run Code Online (Sandbox Code Playgroud)
如何让它看起来像:
1 2 3 4 5 ...
Run Code Online (Sandbox Code Playgroud)
更妙的是,是否可以打印单号在最后一个号码,所以只有一个号码在屏幕上在同一时间?
我是python的新手,我正在编写一些脚本来自动从FTP服务器等下载文件.我想显示下载的进度,但我希望它保持在相同的位置,例如:
输出:
正在下载文件FooFile.txt [47%]
我试图避免这样的事情:
Downloading File FooFile.txt [47%]
Downloading File FooFile.txt [48%]
Downloading File FooFile.txt [49%]
Run Code Online (Sandbox Code Playgroud)
我应该怎么做呢?
我一直想知道人们如何更新命令行中的上一行.一个很好的例子就是在linux中使用wget命令.它会创建一个类似于此的ASCII加载栏:
[======>] 37%
当然,加载条移动并且百分比发生变化,但它不会形成一条新线.我无法弄清楚如何做到这一点.有人能指出我正确的方向吗?
我写一个小程序下载文件通过HTTP(如,例如,描述在这里).
我还想包含一个小的下载进度指示器,显示下载进度的百分比.
这是我想出的:
sys.stdout.write(rem_file + "...")
urllib.urlretrieve(rem_file, loc_file, reporthook=dlProgress)
def dlProgress(count, blockSize, totalSize):
percent = int(count*blockSize*100/totalSize)
sys.stdout.write("%2d%%" % percent)
sys.stdout.write("\b\b\b")
sys.stdout.flush()
输出:MyFileName ... 9%
还有其他想法或建议吗?
有点烦人的是在百分比的第一位数字中终端闪烁的光标.有办法防止这种情况吗?有没有办法隐藏光标?
编辑:
这里有一个更好的替代方法,在dlProgress和'\ r'代码中使用全局变量作为文件名:
global rem_file # global variable to be used in dlProgress
urllib.urlretrieve(rem_file, loc_file, reporthook=dlProgress)
def dlProgress(count, blockSize, totalSize):
percent = int(count*blockSize*100/totalSize)
sys.stdout.write("\r" + rem_file + "...%d%%" % percent)
sys.stdout.flush()
输出:MyFileName ... 9%
并且光标显示在行的END处.好多了.
你如何覆盖python 2.7中的先前打印?我正在制作一个简单的程序来计算pi.这是代码:
o = 0
hpi = 1.0
i = 1
print "pi calculator"
acc= int(raw_input("enter accuracy:"))
if(acc>999999):
print "WARNING: this might take a VERY long time. to terminate, press CTRL+Z"
print "precision: " + str(acc)
while i < acc:
if(o==0):
hpi *= (1.0+i)/i
o = 1
elif(o==1):
hpi *= i/(1.0+i)
o = 0
else:
print "loop error."
i += 1
if i % 100000 == 0:
print str(hpi*2))
print str(hpi*2))
Run Code Online (Sandbox Code Playgroud)
它在100000次计算后基本输出当前的pi.如何让它覆盖以前的计算?
How can I print the status of a Jupyter notebook on the previous line? I think I'm looking for something like clear_output(), but for only a single line.
Sample code:
from IPython.display import clear_output
import time
print('This is important info!')
for i in range(100):
print('Processing BIG data file {}'.format(i))
time.sleep(0.1)
clear_output(wait=True)
if i == 50:
print('Something bad happened on run {}. This needs to be visible at the end!'.format(i))
print('Done.')
Run Code Online (Sandbox Code Playgroud)
When this runs it gives the behavior of over-writing …