Python Storing Data

The built-in pickle module provides some basic functionality for serialization, which is a term for turning arbitrary objects into something suitable to be written to disk. Check out the docs for Python 2 or Python 3.

Pickle isn't very robust though, and for more complex data you'll likely want to look into a database module like the built-in sqlite3 or a full-fledged object-relational mapping (ORM) like SQLAlchemy.


You may try pickle module to store the memory data into disk,Here is an example:

store data:

import pickle
dataset = ['hello','test']
outputFile = 'test.data'
fw = open(outputFile, 'wb')
pickle.dump(dataset, fw)
fw.close()

load data:

import pickle
inputFile = 'test.data'
fd = open(inputFile, 'rb')
dataset = pickle.load(fd)
print dataset

You can make a database and save them, the only way is this. A database with SQLITE or a .txt file. For example:

with open("mylist.txt","w") as f: #in write mode
    f.write("{}".format(mylist))

Your list goes into the format() function. It'll make a .txt file named mylist and will save your list data into it.

After that, when you want to access your data again, you can do:

with open("mylist.txt") as f: #in read mode, not in write mode, careful
    rd=f.readlines()
print (rd)

Tags:

Python

Storage