当我打电话print
从eval
:Python:如何在循环中从`eval`调用`print`?
def printList(myList):
maxDigits = len(str(len(myList)))
Format = '0{0}d'.format(maxDigits)
for i in myList:
eval('print "#{0:' + Format + '}".format(i+1), myList[i]')
它给出了一个错误:
print "#{0:01d}".format(i+1), myList[i]
^
SyntaxError: invalid syntax
我试图利用this,并重新写的:
def printList(myList):
maxDigits = len(str(len(myList)))
Format = '0{0}d'.format(maxDigits)
for i in myList:
obj = compile(src, '', 'exec')
eval('print "#{0:' + Format + '}".format(i+1), myList[i]')
但这个抱怨i
:
NameError: name 'i' is not defined
P.S.我处理python2.6
的Python的版本?除非我误解,否则python 3中的打印语法是不同的。 – Marcin 2012-02-17 14:42:23
为什么使用eval开头? – Gerrat 2012-02-17 14:43:03
这是哪个版本的python? – 2012-02-17 14:43:33