如何从 python 中的 stdout 中删除行?

发布于 2024-09-24 00:06:21 字数 480 浏览 1 评论 0原文

我有一个程序,可以使用 paramiko 通过 ssh 获取一些数据:

ssh = paramiko.SSHClient()

ssh.connect(main.Server_IP, username=main.Username, password=main.Password)

ssh_stdin_host, ssh_stdout_host, ssh_stderr_host =ssh_session.exec_command(setting.GetHostData)

我想从 ssh_stdout_host 中删除前 4 行。我尝试过使用 StringIO 来使用这样的读取行:

output = StringIO("".join(ssh_stdout_host))
data_all = output.readlines()

但在此之后我迷失了。什么是好的方法?我使用 python 2.6.5。谢谢。

I have a program that grabs some data through ssh using paramiko:

ssh = paramiko.SSHClient()

ssh.connect(main.Server_IP, username=main.Username, password=main.Password)

ssh_stdin_host, ssh_stdout_host, ssh_stderr_host =ssh_session.exec_command(setting.GetHostData)

I would like to remove the first 4 lines from ssh_stdout_host. I've tried using StringIO to use readlines like this:

output = StringIO("".join(ssh_stdout_host))
data_all = output.readlines()

But I'm lost after this. What would be a good approach? Im using python 2.6.5. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛歌少女 2024-10-01 00:06:21

如何从 python 中的 stdout 中删除行?

(这是从 stdout Python 控制台窗口 中删除行的一般答案,与涉及 paramiko 的具体问题无关, ssh 等)

另请参阅: 这里此处

不要使用 print 命令或 print() 函数,而是使用 sys.stdout.write ("...") 与 sys.stdout.flush() 结合使用。要删除写入的行,请“返回到上一行”并使用 sys.stdout.write('\r'+' '*n) 以空格覆盖所有字符,其中 n 是行中的字符数。


一个很好的例子说明了一切:

import sys, time

print ('And now for something completely different ...')
time.sleep(0.5)

msg = 'I am going to erase this line from the console window.'
sys.stdout.write(msg); sys.stdout.flush()
time.sleep(1)

sys.stdout.write('\r' + ' '*len(msg))
sys.stdout.flush()
time.sleep(0.5)

print('\rdid I succeed?')
time.sleep(1)

编辑
而不是 sys.stdout.write(msg); sys.stdout.flush(),您还可以使用

print(msg, end='')

对于低于 3.0 的 Python 版本,请将 from __future__ import print_function 放在脚本/模块的顶部以使其起作用。

请注意,此解决方案适用于 stdout Python 控制台窗口,例如,通过右键单击并选择“打开方式 ->”来运行脚本。 Python'。它不适用于 SciTe、Idle、Eclipse 或其他带有合并控制台窗口的编辑器。我正在等待自己的解决方案 此处

How to remove lines from stdout in python?

(this is a general answer for removing lines from the stdout Python console window, and has nothing to do with specific question involving paramiko, ssh etc)

see also: here and here

Instead of using the print command or print() function, use sys.stdout.write("...") combined with sys.stdout.flush(). To erase the written line, go 'back to the previous line' and overwrite all characters by spaces using sys.stdout.write('\r'+' '*n), where n is the number of characters in the line.


a nice example says it all:

import sys, time

print ('And now for something completely different ...')
time.sleep(0.5)

msg = 'I am going to erase this line from the console window.'
sys.stdout.write(msg); sys.stdout.flush()
time.sleep(1)

sys.stdout.write('\r' + ' '*len(msg))
sys.stdout.flush()
time.sleep(0.5)

print('\rdid I succeed?')
time.sleep(1)

edit
instead of sys.stdout.write(msg); sys.stdout.flush(), you could also use

print(msg, end='')

For Python versions below 3.0, put from __future__ import print_function at the top of your script/module for this to work.

Note that this solution works for the stdout Python console window, e.g. run the script by right-clicking and choosing 'open with -> python'. It does not work for SciTe, Idle, Eclipse or other editors with incorporated console windows. I am waiting myself for a solution for that here.

过度放纵 2024-10-01 00:06:21

readlines 提供所有数据

allLines = [line for line in stdout.readlines()]
data_no_firstfour = "\n".join(allLines[4:])

readlines provides all the data

allLines = [line for line in stdout.readlines()]
data_no_firstfour = "\n".join(allLines[4:])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文