相关疑难解决方法(0)

动态打印一行

我想做几个语句,给出标准输出,而不会在语句之间看到换行符.

具体来说,假设我有:

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 printing

283
推荐指数
9
解决办法
49万
查看次数

如何在控制台上的相同位置写入输出?

我是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)

我应该怎么做呢?


重复: 如何在命令行应用程序中打印当前行?

python console-output

148
推荐指数
6
解决办法
16万
查看次数

如何为命令行设置动画?

我一直想知道人们如何更新命令行中的上一行.一个很好的例子就是在linux中使用wget命令.它会创建一个类似于此的ASCII加载栏:

[======>] 37%

当然,加载条移动并且百分比发生变化,但它不会形成一条新线.我无法弄清楚如何做到这一点.有人能指出我正确的方向吗?

command-line

79
推荐指数
3
解决办法
3万
查看次数

如何在Python中编写下载进度指示器?

我写一个小程序下载文件通过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 http

54
推荐指数
3
解决办法
3万
查看次数

python覆盖上一行

你如何覆盖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.如何让它覆盖以前的计算?

python printing pi overwrite python-2.7

11
推荐指数
1
解决办法
2万
查看次数

iPython/ Jupyter notebook clear only one line of output

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 …

python-3.x jupyter-notebook

6
推荐指数
2
解决办法
2992
查看次数