Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing user data in a Python script

Tags:

python

What is the preferred/ usual way of storing data that is entered by the user when running a Python script, if I need the data again the next time the script runs?

For example, my script performs calculations based on what the user enters and then when the user runs the script again, it fetches the result from the last run.

For now, I write the data to a text file and read it from there. I don't think that I would need to store very large records ( less than 100, I'd say).

I am targeting Windows and Linux users both with this script, so a cross platform solution would be good. My only apprehension with using a text file is that I feel it might not be the best and the usual way of doing it.

So my question is, if you ever need to store some data for your script, how do you do it?

like image 811
user225312 Avatar asked Sep 03 '10 03:09

user225312


People also ask

How do you store data in Python?

Using Python's built-in File object, it is possible to write string data to a disk file and read from it. Python's standard library, provides modules to store and retrieve serialized data in various data structures such as JSON and XML. Python's DB-API provides a standard way of interacting with relational databases.

Can you create a database in Python?

Creating a database in MySQL using pythonYou can connect to an existing database or, create your own. You would need special privileges to create or to delete a MySQL database. So if you have access to the root user, you can create any database.


1 Answers

you could use a slite database or a CSV file. They are both very easy to work with but lend themselves to rows with the same type of information.

The best option might be shelve module

import shelve

shelf = shelve.open(filename)
shelf['key1'] = value1
shelf['key2'] = value2

shelf.close()
 # next run
shelf.open(filename)

value1 = shelf['key1']
#etc
like image 123
aaronasterling Avatar answered Oct 16 '22 19:10

aaronasterling