1

Suppose I have the file test.py. I import it using the interactive interpreter, I add some strings to data in my file. How would I go about saving the file afterwards? Closing the interpreter keeps test.py in its original state. I've looked at a couple questions that told me to use pickle. However, one of the issues that was pointed out in the documentation was that pickle is not readable at all. I've tried using the open function, but I'm not sure how I would go about using it effectively.

Bottom line, how would I take data from a python script, add or remove parts of it, and then save it for use afterwards?

test.py:

# test.py
data = []

Python Shell:

>>> import test
>>> test.data.append("Hello There!")
>>> test.data
['Hello There!']
4
  • Don't keep the data in the script, keep it in a different file -- e.g. a .txt or .csv file -- and read and write it in the script. (Also, pickle files are readable -- perhaps you mean not human readable.) Commented Mar 20, 2015 at 20:29
  • The reason as to why I have it set up as a script is because I import it to read the data and perform functions on it. I assume it would be much more complex using a .txt file, since I would have to go through opening it, using the .readlines() method and slice everything up and all. Commented Mar 20, 2015 at 20:37
  • 1
    But to figure out where to write to in your python script you'd have to open it and use the readlines method and all. Pickle is easy in the script, there are lots of automated csv readers if you don't want to use readlines, and anyway you should get competent enough to open and read a text file without thinking it's hard. Commented Mar 20, 2015 at 20:40
  • I think you should take a look at pickle again, here is a link wiki.python.org/moin/UsingPickle Commented Mar 20, 2015 at 23:24

1 Answer 1

3

There is no way to work with Python "data" files while saving them and preserving them as scripts.

While pickle lacks human readability, it's now become my choice in handling data and saving them. This is for the current program that I am writing, as well as future programs to come.

While pickle lacks human readability, at least it is machine readable, meaning that I won't have to go through loads of useless work. It's a compromise for the better.

Thanks to everyone that helped me come to like pickle.

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

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.