[英]How to store a print output to variable?
I wanted to know if there was a way to reproduce an output from one section of a code to the end of the code. 我想知道是否有一种方法可以将输出从代码的一个部分复制到代码的末尾。 So I am assuming I need to assign a variable to the
print output
function. 因此,我假设需要为
print output
功能分配一个变量。 Anyway this is part of my code that I want to store variable output to a variable and reproduce an output anywhere in my code: 无论如何,这是我代码的一部分,我想将变量输出存储到变量并在代码中的任何位置重现输出:
for busnum,busname,scaled_power in busses_in_year[data_location]:
scaled_power= float(scaled_power)
busnum = int(busnum)
output='Bus #: {}\t Area Station: {}\t New Load Total: {} MW\t'
print(output.format(busnum,busname,scaled_power))
You'll need to assign the result of output.format
to a variable; 您需要将
output.format
的结果分配给变量; not the value of the print
function. 不是
print
功能的值。
formatted_output = output.format(busnum, busname, scaled_power)
The print
function will always return None
. print
功能将始终返回None
。 In your loop, if you need the output for each iteration, store them in a list
. 在循环中,如果您需要每次迭代的输出,请将它们存储在
list
。
outputs = []
for busnum, busname, scaled_power in busses_in_year[data_location]:
scaled_power = float(scaled_power)
busnum = int(busnum)
output = 'Bus #: {}\t Area Station: {}\t New Load Total: {} MW\t'
formatted = output.format(busnum, busname, scaled_power)
outputs.append(formatted)
print(formatted)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.