0

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 avgactualwhich 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
lf_celine
  • 653
  • 7
  • 19
  • Doesn't work is too generic. Why it doesn't work? You get any error traceback? If so, please add it to your question. Unexpected behaviour? If so, describe it! – Valentino Jul 03 '19 at 15:46
  • What's `cote_actual`? I don't see it in you data. Is it guaranteed that each `objectID` exists in both data files? – Perplexabot Jul 03 '19 at 16:19
  • I want to add it to each json. Yes, each objectID exists in both data files. – lf_celine Jul 03 '19 at 16:32
  • I edited my question. The problem is that I can't loop through the `cotelv` in my if statement. – lf_celine Jul 04 '19 at 11:06

1 Answers1

1

I believe this answer is found at How can I open multiple files using "with open" in Python?. However, to cut it short, you can open both files in the same row and then perform your actions inside the "with" indentation. The reason your code fails is that after the "with", if you do not indent, the actions will not take into account the opened file, much as a loop or a regular "if" statement. Hence, you can open them simultaneously and edit them under the correct indentation, as shown below.

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())
    #Now that they are opened, you can perform the actions through here

Hope that answers your question!

Preto
  • 78
  • 1
  • 6