I am trying to write a script like this in Python:
Let's say I have two JSON files, one has a data structure like this :
fruitswithprice.json
{
fruit: "banana",
color: "yellow",
price : 20,
}
the other is fruitsnoprice.json
{
fruit: "banana",
color: "yellow",
}
I want to append the price field to fruitsnoprice.json for every entry that the key "fruit" matches.
I tried this :
new_dict = []
def add_price(json_file):
with open("../../dictionary/original.json", "r", encoding="utf-8") as original_dictionary, open(json_file, "r", encoding="utf-8-sig") as word_list:
dictionary = json.load(original_dictionary)
list_to_append = json.load(word_list)
for word in dictionary:
for wInList in list_to_append:
if word['fruit'] == wInList['fruit']:
wInList['price'] == word['price']
new_dict.append(wInList)
print(new_dict)
getting an error at wInList['price'] == word['price'].
CodePudding user response:
You can't do this: wInList['price'] == word['price']
.
You can assign it like this:
wInList['price'] = word['price'] # Notice! Just one '='