我想知道如何将 print 的输出分配给变量。
so if
mystring = "a=\'12\'"
then
print mystring
a=12
我想像 **kwargs 一样传递这个,
test(mystring)
我怎样才能做到这一点?
有关更多解释:我有一个从数据文件的注释行获得的字符串列表。它看起来像这样:
"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'",
'']
我想将这些值放入某种结构中,以便我可以绘制各种事物与任何变量的关系,因此该列表基本上是一个图例,并且我想绘制迹线的函数与图例中给出的变量。
因此,如果对于每个条目我都有一个跟踪,那么我可能想绘制一系列 a 值的 max(trace) 与 offX 。
我的第一个想法是将字符串作为 **kwargs 传递给一个函数,该函数将生成相应数据的矩阵。
重定向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
然后只需从中获取“输出”foo.content
并用它做你想做的事。
如果我误解了您的要求,请忽略。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)