Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to store data in Python? [closed]

I'm creating a Skype bot for a group of friends, and I want to be able to have a somewhat login system for text-based games and storing information such as usernames, high scores, notes, friends list, messages, etc.

I was thinking of storing it in a text file named after the person's handle on Skype, however, I was wondering if there was a better way of doing it. Such as XML files.

I'd like to avoid SQL servers, and it's not like they're storing passwords so encryption won't be that much of a big deal. (I'd prefer local file storage. Something easily editable and delete-able)

I want to enable commands such as !note and !friends and !addfriend and !highscore and so on, but I need a method to save that information.

like image 358
Axiom Avatar asked Apr 12 '14 09:04

Axiom


People also ask

Where do you store data in Python?

Data so received, is stored in computer's main memory (RAM) in the form of various data structures such as, variables and objects until the application is running. Thereafter, memory contents from RAM are erased.

How we can store data from your Python programs even when they are not running?

Depending on your needs, you can either save the information to a text file or use a database. Saving to a text file doesn't require any encoding, however two popular formats/libraries for python are json and pickle. If you want to use a database instead I would recommend looking at either mysql or sqlite.


1 Answers

Have you considered pickle? It can store python objects (any object) to files so you can just load and use them.

import pickle

# Saving
data = [1,2,3,4]
pickle.dump(data, open("d:/temp/test.pkl","wb"))

# Loading
load = pickle.load(open("d:/temp/test.pkl","rb"))

For more info, read the docs

(Another option is the json module which serializes to json. It is used in a similar way, but can only save dictionaries, lists, strings and integers)

like image 200
tmrlvi Avatar answered Sep 20 '22 18:09

tmrlvi