在嵌套循环的函数内部使用exec()

时间:2019-03-07 12:03:13

标签: python exec

我正在尝试使用exec()为变量分配字符串和日期值。 我最初是得到一个

  

SyntaxError: unqualified exec is not allowed in function 'getinfo' it contains a nested function with free variables when I tried to execute it like exec('person_{} = entries[index].who.encode("UTF8")' ).format(number_of_credits)

我已经尝试了所有方法,但在使用globals(),locals()时却没有任何错误,但是不会产生输出。

def getinfo(client, ticket):
  if ticket.number ==3: 
    stuck = list(response.ticket.approval.approvers)
    for i in stuck:
      if "status: CURRENT_PENDING" in str(i):
        stuck_on= str(i).splitlines()
        stuck_on= stuck_on[1]
        stuck_on= stuck_on.replace('assignees: ', "")
        stuck_on= stuck_on.replace('"', "")  
    entries= response.ticket.core.worklog.entries   
    number_of_approvals = 0
    index =-1
    for e in entries:
      index+=1
      if 'Credit' in e.comment:
        number_of_credits +=1
        credits=  'person_{}'.format(number_of_credits)
        credit_date = 'person_date_{}'.format(number_of_credits)
        _locals = locals()
        exec('credits = entries[index].who.encode("UTF8")', globals(), _locals)
        exec('credit_date = time.strftime("%Y-%m-%d", time.localtime(int(str(entries[index].when)[:-3])))', globals(), _locals)

我想为尽可能多的人和日期分配值。即person_1 =条目[index] .who.encode(“ UTF8”)的值为'Bob',并且person_date_1 ='2009-01-08'

0 个答案:

没有答案