Python Forum
Updating dictionary in another py file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Updating dictionary in another py file
#1
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!
Reply
#2
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.
Larz60+ and SheeppOSU like this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 1,131 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Using dictionary to find the most sent emails from a file siliusu 6 7,452 Apr-22-2021, 06:07 PM
Last Post: siliusu
  Making a dictionary from a file instyabam 0 1,481 Oct-27-2020, 11:59 AM
Last Post: instyabam
  how can i create a dictionary of dictionaries from a file Astone 2 2,209 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  Convert all actions through functions, fill the dictionary from a file Astone 3 2,369 Oct-26-2020, 09:11 AM
Last Post: DeaD_EyE
  how to put text file in a dictionary infected400 2 2,956 Jan-06-2019, 04:43 PM
Last Post: micseydel
  Dictionary to .txt or .csv file stanthaman42 9 4,563 Aug-08-2018, 03:37 PM
Last Post: Vysero
  dictionary from file Zatoichi 5 3,775 Feb-11-2018, 07:15 PM
Last Post: j.crater
  Dictionary + File Reading palmtrees 2 4,817 Nov-15-2016, 05:16 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020