2

I have a program that imports a .py file that contains lists and dictionaries and uses them in the program. I am making another program that's purpose is to change the lists and dictionaries in this database .py file (either adding or removing parts of the lists/dictionaries). How would I go about doing this? Do i need to read in the .py file line by line, modify the lists, and overwrite the document? Is there a better way?

Any ideas would be much appreciated. If overwriting the file is the best plan, how do you do that?

5
  • 2
    is there a reason your DB is inside the .py-File? You might think about changing that. You could use a sqlite-DB or store data in a CSV-File, that saves you from that problem. Commented Apr 29, 2014 at 13:30
  • @kratenko From what I can understand, he isn't talking about huge amount of data. Just some lists/dictionaries and for that, in my opinion DBs are an overkill Commented Apr 29, 2014 at 13:36
  • That is correct. It just contains about 15 dictionaries and lists. Commented Apr 29, 2014 at 13:40
  • The reason it is a .py file is so that it can also be edited by hand by people who have little knowledge of coding. Commented Apr 29, 2014 at 13:41
  • @cole Well - then my next suggestion would have been JSON - but I think someone did that already... btw: keeping data away from code, if someone else is going to edit the data is always a good idea. Decreases the chances, other people break your program code. Commented Apr 29, 2014 at 14:54

2 Answers 2

4

Instead of directly editing py files within other py files, you should use the Pickle module. Pickle module allows you to save objects (dictionaries, lists, etc.) in a file which is binary formatted. Then you can fetch those dictionaries or whatever you saved, from the file itself without anything else.

You can also use the JSON module if you want pure simplicity and data interchange.

Here's how you can do something similar with the JSON module:

>> import json
>> a = {'foo': 'bar', 'hello': 'jello'}
>> json.dumps(a)
>> '{"foo": "bar", "hello": "jello"}'

NOTE: The json module will turn any single quote into double quotes as JSON itself does not accept single quote. And also notice the 's' after 'dump'. It makes sure that the function returns pure string rather than any binary format.

Hope, that helps you :D

Sign up to request clarification or add additional context in comments.

3 Comments

JSON is a better solution than pickle for just lists and dictionaries. Pickle is fairly awful.
@Sazid Will i have to use a json file type? Or will this module work with a .py file.
@Cole No, you can use any extension you like :) The same applies for the pickle module too
0

You can use SQLite or Pickle module instead, to allow easier data retrieval/manipulation from multiple programs/scripts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.