У меня есть запрос 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», если в БД существует значение «target». Моя проблема в том, чтобы иметь возможность делать что-то еще, когда в ответе не существует «целевого» значения.
Вот что я сделал до сих пор:
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», но выводом является бесконечный цикл печати («Не существует»).
Самое близкое, что я нашел к решению, это:
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 <----
}]
Ваша помощь будет высоко оценена. Спасибо!
EDIT
Приведенный выше код касается только одной переменной * 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
}]