1

I have 2 python files, file1.py has only 1 dictionary and I would like to read & write to that dictionary from file2.py. Both files are in same directory.

I'm able to read from it using import file1 but how do I write to that file.

Snippet:

file1.py (nothing additional in file1, apart from following data)

dict1 = {
        'a' : 1,      # value is integer
        'b' : '5xy',   # value is string
        'c' : '10xy',
        'd' : '1xy',
        'e' : 10,
        }

file2.py

    import file1
    import json

    print file1.dict1['a']    #this works fine
    print file1.dict1['b']

    # Now I want to update the value of a & b, something like this:

    dict2 = json.loads(data)
    file1.dict1['a'] = dict2.['some_int']     #int value
    file1.dict1['b'] = dict2.['some_str']     #string value

The main reason why I'm using dictionary and not text file, is because the new values to be updated come from a json data and converting it to a dictionary is simpler saving me from string parsing each time I want to update the dict1.

Problem is, When I update the value from dict2, I want those value to be written to dict1 in file1

Also, the code runs on a Raspberry Pi and I've SSH into it using Ubuntu machine.

Can someone please help me how to do this?

EDIT:

  1. file1.py could be saved in any other format like .json or .txt. It was just my assumption that saving data as a dictionary in separate file would allow easy update.
  2. file1.py has to be a separate file, it is a configuration file so I don't want to merge it to my main file.
  3. The data for dict2 mention above comes from socket connection at

dict2 = json.loads(data)

  1. I want to update the *file1** with the data that comes from socket connection.
keyur
  • 77
  • 1
  • 6
  • Why don't you write the data to a JSON that is read by both files? As far as I can tell you don't gain anything from having one python file edit another, that just seems really unnecessary – SAMO Aug 01 '16 at 19:43

4 Answers4

1

I think you want to save the data from file1 into a separate .json file, then read the .json file in your second file. Here is what you can do:

file1.py

import json
dict1 = {
        'a' : 1,      # value is integer
        'b' : '5xy',   # value is string
        'c' : '10xy',
        'd' : '1xy',
        'e' : 10,
        }
with open("filepath.json", "w+") as f:
    json.dump(dict1, f)

This will dump the dictionary dict1 into a json file which is stored at filepath.json.

Then, in your second file:

file2.py

import json

with open("pathname.json") as f:
    dict1 = json.load(f)

# dict1 = {
        'a' : 1,      # value is integer
        'b' : '5xy',   # value is string
        'c' : '10xy',
        'd' : '1xy',
        'e' : 10,
        }

dict1['a'] = dict2['some_int']     #int value
dict1['b'] = dict2['some_str']     #string value

Note: This will not change the values in your first file. However, if you need to access the changed values, you can dump your data into another json file, then load that json file again whenever you need the data.

Peter Wang
  • 1,808
  • 1
  • 11
  • 28
1

If you are attempting to print the dictionary back to the file, you could use something like...

outFile = open("file1.py","w")
outFile.writeline("dict1 = " % (str(dict2)))
outFile.close()

You might be better off having a json file, then loading the object from and writing the object value back to a file. You could them manipulate the json object in memory, and serialize it simply.

Z

Zaren
  • 106
  • 3
  • Thanks Zaren, as you suggested I stored my data into a json file instead of a dictionary in python file. It made my work a lot simpler. – keyur Aug 02 '16 at 00:22
0

You should use the pickle library to save and load the dictionary https://wiki.python.org/moin/UsingPickle

Here is the basic usage of pickle

   1 # Save a dictionary into a pickle file.
   2 import pickle
   3 
   4 favorite_color = { "lion": "yellow", "kitty": "red" }
   5 
   6 pickle.dump( favorite_color, open( "save.p", "wb" ) )



   1 # Load the dictionary back from the pickle file.
   2 import pickle
   3 
   4 favorite_color = pickle.load( open( "save.p", "rb" ) )
   5 # favorite_color is now { "lion": "yellow", "kitty": "red" }
James Russo
  • 578
  • 3
  • 18
  • Thanks James, I looked into pickle, it would have been useful if I had to write a full dictionary all the time. But in my case, I'm won't be sure if I had to write 1 or all values. – keyur Aug 02 '16 at 00:20
0

Finally as @Zaren suggested, I used a json file instead of dictionary in python file.

Here's what I did:

  1. Modified file1.py to file1.json and store the data with appropriate formatting.

  2. From file2.py, I opened file1.json when needed instead of import file1 and used json.dump & json.load on file1.json

keyur
  • 77
  • 1
  • 6