Apr-02-2020, 04:57 PM
I am trying to add key\value pairs to a Json file. I can write one pair to the file but I can't figure out how to append additional pairs. When I try, it just replaces the current pair. I have tried following this tutorial but I get key errors.
console output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import json product_data = "{}" product = input ( "Enter product name. " ) price = input ( "enter product price. " ) new_product = {product:price} load = json.loads(product_data) load.update(new_product) with open ( "Product.json" , "r+" ) as file : json.dump(load, file ) |
Output:Enter product name. milk
enter product price. 2.99
File output:Output:{"milk": "2.99"}