我想做几个语句,给出标准输出,而不会在语句之间看到换行符.
具体来说,假设我有:
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)
更妙的是,是否可以打印单号在最后一个号码,所以只有一个号码在屏幕上在同一时间?
在Windows中使用python的curses模块有什么替代方法吗?我查看了python文档,但是它提到了它在unix中的使用.我对这些不是很熟悉,所以有没有办法在windows中使用curses模块或者是否有一些专门用于windows的类似模块?[我使用的是Python 3.3]
在Unix上,我可以使用\ r(回车)或\ b(退格)来打印在shell中已经可见的文本(即再次覆盖当前行).
我可以从Python脚本在Windows命令行中实现相同的效果吗?
我尝试了curses模块,但它似乎在Windows上不可用.
相比:
for item in range(0, 5):
sys.stdout.write('c')
for item in range(0, 5):
sys.stdout.write('\b')
Run Code Online (Sandbox Code Playgroud)
像你想象的那样工作,但是:
for item in range(0, 5):
sys.stdout.write('\n')
for item in range(0, 5):
sys.stdout.write('\b')
Run Code Online (Sandbox Code Playgroud)
仍然留下五个换行符.有任何想法吗?
我使用Python 3.4上传FTP文件.
我希望能够在上传文件时显示进度百分比.这是我的代码:
from ftplib import FTP
import os.path
# Init
sizeWritten = 0
totalSize = os.path.getsize('test.zip')
print('Total file size : ' + str(round(totalSize / 1024 / 1024 ,1)) + ' Mb')
# Define a handle to print the percentage uploaded
def handle(block):
sizeWritten += 1024 # this line fail because sizeWritten is not initialized.
percentComplete = sizeWritten / totalSize
print(str(percentComplete) + " percent complete")
# Open FTP connection
ftp = FTP('website.com')
ftp.login('user','password')
# Open the file and upload it …Run Code Online (Sandbox Code Playgroud) 我喜欢在IJulia笔记本上工作,并希望一遍又一遍地在同一行上打印某些流程的状态.
以下面链接中给出的示例为例,我们需要一些输出:
Downloading File FooFile.txt [47%]
Run Code Online (Sandbox Code Playgroud)
并希望避免这样的事情:
Downloading File FooFile.txt [47%]
Downloading File FooFile.txt [48%]
Downloading File FooFile.txt [49%]
Run Code Online (Sandbox Code Playgroud)
在Python的情况下,我在这里找到了答案.
什么是解决方案?
我正在尝试更新PyCharm控制台中的最后一行.说,我打印a然后我想将其更改为c.但是,我遇到了以下问题.当我跑:
print 'a\bc'
Run Code Online (Sandbox Code Playgroud)
它打印
a c
Run Code Online (Sandbox Code Playgroud)
而所需的输出(这也是我在Windows控制台中看到的)是:
c
Run Code Online (Sandbox Code Playgroud)
有没有办法将光标移回PyCharm的控制台?或者可能删除整行?
我想编写一个 python 脚本,在终端的最后一行显示其当前进度,类似于apt(not apt-get) 的行为。我知道有 ANSI 控制序列可以与终端交互,但问题是我想调用其他程序(使用subprocess)来生成自己的输出。我不想让这些程序覆盖我的进度。
可能有多个子进程同时运行。我正在使用该multithreading模块,每个线程都会执行一些工作,然后调用并等待子进程,然后执行清理工作。我不介意子进程的输出被混合,只要最后一行不受影响。
当程序退出时,最后一行应该被清除。
python ×7
backspace ×1
command-line ×1
console ×1
curses ×1
julia ×1
linux ×1
overwrite ×1
printing ×1
pycharm ×1
python-3.4 ×1
subprocess ×1
terminal ×1
windows ×1