Python Forum

Full Version: Updating dictionary in another py file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I have 2 py files. Stock.py and data.py. I use a dictionary from the data file and would like to update it from within the stock file.

For example, the following code is in the stock file. All items in the dictionary +1.
from data import stuk

for cat in stuk:
    stuk[cat] = stuk[cat] + 1

print(stuk)
The data file contains:
stuk = {"hoeksalon":0,
        "fauteuils":0,
        "Relax zetels":0,
        "Salontafel":0,
        "bijzettafel":0,
        "Zetels":0,
        "Slaapbanken":0
        }
How do I update the data file?

I googled this for a while, but I can not figure it out...

Any help would be greatly appreciated!
don't store the dict in a py file.
Store it in a file, e.g. json and load it, then dump it back when you want to preserve the new data.
Of course, JSON is just an example, you can use simple text file, a db, etc.