我有一个JSON查询,该查询返回要修改的词典列表。这是响应中令我感兴趣的部分:
{
#...
"publisher_bid_modifier": {
"values": [{
"target": "msn-can",
"cpc_modification": 1.5
},
{
"target": "msn-can-home",
"cpc_modification": 1.5
}
]
}
}
以下代码中的打印(温度)将返回:
[{"target": "msn-can-home","cpc_modification": 0.5}, {"target": "msn-can","cpc_modification": 0.5}]
然后,我从数据库中提取要修改的数据,并将其与从JSON响应中提取的数据进行匹配。
如果数据库中存在“目标”的值,那么我可以轻松地修改“ cpc_modification”。我的问题是,当响应中不存在“目标”值时,能够执行其他操作。
这是我到目前为止所做的:
print(temp)
for dt in temp:
theCursor.execute('SELECT ADJUST, SITE FROM SandboxPubTB WHERE CAMPAIGNNAME =?', (campaignName,) )
campaignRows = theCursor.fetchall()
for t in campaignRows:
if t[1] == dt['target'] :
dt['cpc_modification'] = "{:.2f}".format((int(t[0]) / 100) + 1)
print("exists")
#if dt['target'] not in t[1] :
else:
temp.append({'target': '"' + t[1] + "'", 'cpc_modification': "'" + str(t[0]) + "'"})
print("Doesn't exists")
print(temp)
在其他情况下,我试图用新的“ target”和“ cpc_modification”附加新的列表条目,但输出是print的无限循环(“不存在”)。
我最接近解决方案的是:
elif dt['target'] not in temp:
但这将迭代与临时列表中条目数一样多的时间。
举例说明输入和输出:
输入:
[{
'target': 'msn-can',
'cpc_modification': 1.5
}, {
'target': 'msn-can-home',
'cpc_modification': 1.5
}, {
'target': 'foxnews-foxnews',
'cpc_modification': 1.5
}, {
'target': 'beachraider',
'cpc_modification': 0.69
}, {
'target': 'crowdyfan',
'cpc_modification': 0.7
}, {
'target': 'novelodge',
'cpc_modification': 0.75
}, {
'target': 'foxnews-androidapp',
'cpc_modification': 0.5
}, {
'target': 'foxnews-foxbusiness',
'cpc_modification': 1.12
}, {
'target': 'foxnews-iosapp',
'cpc_modification': 0.86
}, {
'target': 'thehindu-hindunews',
'cpc_modification': 0.7
}, {
'target': 'vitaminnews',
'cpc_modification': 1.46
}]
数据库:
输出:
[{
'target': 'msn-can',
'cpc_modification': 0.5 <----
}, {
'target': 'msn-can-home',
'cpc_modification': 0.5 <----
}, {
'target': 'foxnews-foxnews',
'cpc_modification': 1.5
}, {
'target': 'beachraider',
'cpc_modification': 0.69
}, {
'target': 'crowdyfan',
'cpc_modification': 0.7
}, {
'target': 'novelodge',
'cpc_modification': 0.75
}, {
'target': 'foxnews-androidapp',
'cpc_modification': 0.5
}, {
'target': 'foxnews-foxbusiness',
'cpc_modification': 1.12
}, {
'target': 'foxnews-iosapp',
'cpc_modification': 0.86
}, {
'target': 'thehindu-hindunews',
'cpc_modification': 0.7
}, {
'target': 'vitaminnews',
'cpc_modification': 1.46
}], {
'target': 'msn-outlookcom-canada', <----
'cpc_modification': 0.5 <----
}]
非常感谢您的帮助。谢谢!
编辑
上面的代码仅处理一个广告系列* campaignName变量”,但是整个代码应能够处理多个广告系列。因此,这是另一个示例:
输入
广告系列1:
[{
'target': 'msn-can',
'cpc_modification': 0.5
}, {
'target': 'msn-can-home',
'cpc_modification': 0.5
}]
广告系列2:
[{
'target': 'fox-news',
'cpc_modification': 0.9
}, {
'target': 'fox-news-home',
'cpc_modification': 0.6
}]
数据库中的数据
广告系列1:
target: msn-can, cpc_modification: 7
target: msn-can-home, cpc_modification: 10
广告系列2:
target: fox-news, cpc_modification: 20
target: fox-news-home, cpc_modification: 30
target: msn-us, cpc_modification: 10
输出
广告系列1:
[{
'target': 'msn-can',
'cpc_modification': 1.07
}, {
'target': 'msn-can-home',
'cpc_modification': 1.1
}]
广告系列2:
[{
'target': 'fox-news',
'cpc_modification': 1.2
}, {
'target': 'fox-news-home',
'cpc_modification': 1.3
}], {
'target': 'msn-us',
'cpc_modification': 1.1
}]
答案 0 :(得分:0)
重新分组数据,以便您有一个由目标键锁定的字典。
message
那么您就可以自然地查找数据
drive.ListFile({'q':"'<Team_Drive_Id_Or_Folder_Id>' in parents and trashed=false", 'corpora': 'teamDrive', 'teamDriveId': '<Team_Drive_Id>', 'includeTeamDriveItems': True, 'supportsTeamDrives': True}).GetList()