将print的输出分配给python中的变量

ale*_*lex 2 python

我想知道如何将print的输出分配给变量.

因此,如果

mystring = "a=\'12\'"
Run Code Online (Sandbox Code Playgroud)

然后

print mystring 
a=12
Run Code Online (Sandbox Code Playgroud)

我想像**kwargs一样传递这个

test(mystring)
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

更多解释:我有一个从数据文件的注释行获得的字符串列表.它看起来像这样:

"a='0.015in' lPrime='0.292' offX='45um' offY='75um' sPrime='0.393' twistLength='0'",
 "a='0.015in' lPrime='0.292' offX='60um' offY='75um' sPrime='0.393' twistLength='0'",
 "a='0.015in' lPrime='0.292' offX='75um' offY='75um' sPrime='0.393' twistLength='0'",
 '']
Run Code Online (Sandbox Code Playgroud)

我想把这些值放到某个结构中,这样我就可以绘制各种不同的变量,所以列表基本上是一个图例,我想绘制轨迹的函数与传奇中给出的变量.

因此,如果对于每个条目我有一个跟踪,那么我可能想要为一系列值绘制max(trace)vs offX.

我的第一个想法是将字符串作为**kwargs传递给一个能产生相应数据矩阵的函数.

jld*_*ont 6

重定向stdout并捕获对象中的输出?

import sys

# a simple class with a write method
class WritableObject:
    def __init__(self):
        self.content = []
    def write(self, string):
        self.content.append(string)

# example with redirection of sys.stdout
foo = WritableObject()                   # a writable object
sys.stdout = foo                         # redirection

print "one, two, three, four"            # some writing
Run Code Online (Sandbox Code Playgroud)

然后从中获取"输出" foo.content并用它做你想做的事.

如果我误解了你的要求,请不要理会.