自定义变量 exec({}={})
app_ids = ['12','13','26']
for app_id in app_ids:
exec('clkmap{} = {}'.format(abs(int(app_id)), {})) # 自定义变量
exec('impmap{} = {}'.format(abs(int(app_id)), {}))
赋值自定义变量 exec({}={})
for app_id in app_ids:
with open('{}/{}/click_fm/{}/click_{}.log'.format(ddir,str(abs(int(app_id))),date,str(app_id))) as f:
cli_fm = f.readlines()[-1] #取log文件的最后一行
cli_fm = cli_fm.split(',') # 以逗号分割
cli_list = cli_fm[0:-1] # 去掉log日志的最后一行的最后一句话“wantlsd from heart!\n”
for l in cli_list:
target = l.split(' = ')[0].split("'")[1] # key值
if target == 'oe':
value = l.split(' = ')[1:][0].split(' ')[0] # value值
else:
value = l.split(' = ')[-1]
eval('clkmap'+str(abs(int(app_id))))[target] = value
f.close()
输出自定义变量的值 eval()
print('app_id为{}的点击率指标为:{}'.format(app_id, eval('clkmap' + str(abs(int(app_id))))))
举例,click_12.log文件的图例:
click_12.log