I have two json files with price values. Each of these two values are in each file. The second one corresponds to the average value of other prices (which is count in a "soldnumber" key. What I'd like is to create an if statement where: if sold number > 3, then add the "avgactualeu" value to "cote_actual". Else: add the cote_lv value.
Sample of the first database:
[{
"objectID": 10000,
"cote_lv": 28000,
},
{
"objectID": 10001,
"cote_lv": 35000,
}...]
Sample of the second one:
[{
"objectID": 10002,
"avg_actual": 47640,
"sold_number": 2,
},
{
"objectID": 10001,
"sold_number": 5,
"unsold_number": 1,
"unsold_var": 17
}...]
I expect an output with a "cote_actual": value for each objectID in the two files.
My python code that doesn't work:
import json
with open('./output/gmstatsencheresdemo.json', encoding='utf-8') as data_file2, open('./live_files/demo_db_live.json', encoding='utf-8') as data_file:
data2 = json.loads(data_file2.read())
data = json.loads(data_file.read())
for i in data:
cotelv = i.get('cote_lv')
i['cote_actual'] = {}
i['cote_actual'] = cotelv
for x in data2:
soldnumber = x.get('sold_number')
avgactual = x.get('avg_actual')
x['cote_actual'] = {}
x['cote_actual'] = avgactual
if soldnumber >= 3:
x['cote_actual'] = avgactual
elif soldnumber <= 3:
x['cote_actual'] = cotelv
print(x['cote_actual'])
EDIT: My output is okay when it comes to avgactual
which is display correctly but it's not with cotelv
: it doesn't loop through all the values but only display one (9000 in this example)
Output:
9000
107792
9000
125700
9000