如何使用python打印输出?

时间:2022-06-29 02:23:52

When this .exe file runs it prints a screen full of information and I want to print a particular line out to the screen, here on line "6":

当这个.exe文件运行时,它会打印一个充满信息的屏幕,我想在屏幕上打印一条特定的行,这里是“6”行:

    cmd =  ' -a ' + str(a) + ' -b ' + str(b) + str(Output)
    process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
    outputstring = process.communicate()[0]
    outputlist = outputstring.splitlines()
    Output = outputlist[5]
    print cmd

This works fine: cmd = ' -a ' + str(a) + ' -b ' + str(b)

这很好用:cmd =' - a'+ str(a)+' - b'+ str(b)

This doesn't work: cmd = ' -a ' + str(a) + ' -b ' + str(b) + str(Output)

这不起作用:cmd =' - a'+ str(a)+' - b'+ str(b)+ str(输出)

I get an error saying Output isn't defined. But when I cut and paste:

我收到一个错误,说输出没有定义。但是当我剪切和粘贴时:

    outputstring = process.communicate()[0]
    outputlist = outputstring.splitlines()
    Output = outputlist[5]

before the cmd statement it tells me the process isn't defined. str(Output) should be what is printed on line 6 when the .exe is ran.

在cmd语句之前它告诉我进程没有定义。 str(输出)应该是.exe运行时在第6行打印的内容。

4 个解决方案

#1


You're trying to append the result of a call into the call itself. You have to run the command once without the + str(Output) part to get the output in the first place.

您试图将呼叫结果附加到呼叫本身。您必须在没有+ str(输出)部分的情况下运行命令一次以获得输出。

Think about it this way. Let's say I was adding some numbers together.

这样想吧。假设我在一起添加一些数字。

 z = 5 + b
 b = z + 2

I have to define either z or b before the statements, depending on the order of the two statements. I can't use a variable before I know what it is. You're doing the same thing, using the Output variable before you define it.

我必须在语句之前定义z或b,具体取决于两个语句的顺序。在我知道它是什么之前我不能使用变量。在定义它之前,使用Output变量执行相同的操作。

#2


It's not supposed to be a "dance" to move things around. It's a matter of what's on the left side of the "=". If it's on the left side, it's getting created; if it's on the right side it's being used.

移动事物不应该是一种“舞蹈”。这是“=”左侧的问题。如果它在左侧,它就会被创建;如果它在右侧则被使用。

As it is, your example can't work even a little bit because line one wants part of output, which isn't created until the end.

事实上,你的例子甚至无法工作,因为第一行需要输出的一部分,直到最后才创建。

The easiest way to understand this is to work backwards. You want to see as the final result?

理解这一点的最简单方法是向后工作。你想看到最终结果?

print output[5]

Right? So to get there, you have to get this from a larger string, right?

对?所以要到达那里,你必须从一个更大的字符串得到它,对吧?

output= outputstring.splitlines()
print output[5]

So where did outputstring come from? It was from some subprocess.

那么outputstring来自哪里?它来自某个子流程。

outputstring = process.communicate()[0]
output= outputstring.splitlines()
print output[5]

So where did process come from? It was created by subprocess Popen

那么流程来自哪里?它是由子进程Popen创建的

process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
outputstring = process.communicate()[0]
output= outputstring.splitlines()
print output[5]

So where did cmd come from? I can't tell. Your example doesn't make sense on what command is being executed.

那么cmd来自哪里?我说不出来。您的示例对正在执行的命令没有意义。

cmd = ?  
process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
outputstring = process.communicate()[0]
output= outputstring.splitlines()
print output[5]

#3


Just change your first line to:

只需将第一行更改为:

cmd = ' -a ' + str(a) + ' -b ' + str(b)

cmd =' - a'+ str(a)+' - b'+ str(b)

and the print statement at the end to:

以及最后的印刷声明:

print cmd + str(Output)

print cmd + str(输出)

This is without knowing exactly what it is you want to print... It -seems- as if your problem is trying to use Output before you actually define what the Output variable is (as the posts above)

这是在不确切地知道你想要打印的内容......看起来 - 似乎你的问题是在你真正定义Output变量之前尝试使用Output(如上面的帖子)

#4


Like you said, a variable has to be declared before you can use it. Therefore when you call str(Output) ABOVE Output = outputlist[5], Output doesn't exist yet. You need the actually call first:

就像你说的,必须先声明一个变量才能使用它。因此,当调用str(输出)ABOVE Output = outputlist [5]时,输出尚不存在。你需要先实际调用:

cmd =  ' -a ' + str(a) + ' -b ' + str(b)

then you can print the output of that command:

然后你可以打印该命令的输出:

cmd_return =  ' -a ' + str(a) + ' -b ' + str(b) + str(Output)

should be the line directly above print cmd_return.

应该是打印cmd_return正上方的行。

#1


You're trying to append the result of a call into the call itself. You have to run the command once without the + str(Output) part to get the output in the first place.

您试图将呼叫结果附加到呼叫本身。您必须在没有+ str(输出)部分的情况下运行命令一次以获得输出。

Think about it this way. Let's say I was adding some numbers together.

这样想吧。假设我在一起添加一些数字。

 z = 5 + b
 b = z + 2

I have to define either z or b before the statements, depending on the order of the two statements. I can't use a variable before I know what it is. You're doing the same thing, using the Output variable before you define it.

我必须在语句之前定义z或b,具体取决于两个语句的顺序。在我知道它是什么之前我不能使用变量。在定义它之前,使用Output变量执行相同的操作。

#2


It's not supposed to be a "dance" to move things around. It's a matter of what's on the left side of the "=". If it's on the left side, it's getting created; if it's on the right side it's being used.

移动事物不应该是一种“舞蹈”。这是“=”左侧的问题。如果它在左侧,它就会被创建;如果它在右侧则被使用。

As it is, your example can't work even a little bit because line one wants part of output, which isn't created until the end.

事实上,你的例子甚至无法工作,因为第一行需要输出的一部分,直到最后才创建。

The easiest way to understand this is to work backwards. You want to see as the final result?

理解这一点的最简单方法是向后工作。你想看到最终结果?

print output[5]

Right? So to get there, you have to get this from a larger string, right?

对?所以要到达那里,你必须从一个更大的字符串得到它,对吧?

output= outputstring.splitlines()
print output[5]

So where did outputstring come from? It was from some subprocess.

那么outputstring来自哪里?它来自某个子流程。

outputstring = process.communicate()[0]
output= outputstring.splitlines()
print output[5]

So where did process come from? It was created by subprocess Popen

那么流程来自哪里?它是由子进程Popen创建的

process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
outputstring = process.communicate()[0]
output= outputstring.splitlines()
print output[5]

So where did cmd come from? I can't tell. Your example doesn't make sense on what command is being executed.

那么cmd来自哪里?我说不出来。您的示例对正在执行的命令没有意义。

cmd = ?  
process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
outputstring = process.communicate()[0]
output= outputstring.splitlines()
print output[5]

#3


Just change your first line to:

只需将第一行更改为:

cmd = ' -a ' + str(a) + ' -b ' + str(b)

cmd =' - a'+ str(a)+' - b'+ str(b)

and the print statement at the end to:

以及最后的印刷声明:

print cmd + str(Output)

print cmd + str(输出)

This is without knowing exactly what it is you want to print... It -seems- as if your problem is trying to use Output before you actually define what the Output variable is (as the posts above)

这是在不确切地知道你想要打印的内容......看起来 - 似乎你的问题是在你真正定义Output变量之前尝试使用Output(如上面的帖子)

#4


Like you said, a variable has to be declared before you can use it. Therefore when you call str(Output) ABOVE Output = outputlist[5], Output doesn't exist yet. You need the actually call first:

就像你说的,必须先声明一个变量才能使用它。因此,当调用str(输出)ABOVE Output = outputlist [5]时,输出尚不存在。你需要先实际调用:

cmd =  ' -a ' + str(a) + ' -b ' + str(b)

then you can print the output of that command:

然后你可以打印该命令的输出:

cmd_return =  ' -a ' + str(a) + ' -b ' + str(b) + str(Output)

should be the line directly above print cmd_return.

应该是打印cmd_return正上方的行。