用Python存储数据的最佳方法是什么? [英] What is the best way to store data in Python?

查看:52
本文介绍了用Python存储数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一群朋友创建一个Skype机器人,并且我希望能够拥有一个用于基于文本的游戏的登录系统并存储诸如用户名,高分,注释,朋友列表,消息,等等

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.

我当时正在考虑将其存储在以该人在Skype上的手柄命名的文本文件中,但是,我想知道是否有更好的方法可以做到这一点.如XML文件.

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.

我想避免使用SQL服务器,这并不是因为它们存储密码,所以加密不会有什么大不了的.(我希望使用本地文件存储.一些易于编辑和删除的东西)

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)

我想启用诸如!note和!friends以及!addfriend和!highscore之类的命令,但是我需要一种方法来保存该信息.

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.

非常感谢.:)

推荐答案

您是否考虑过 pickle ?它可以将python对象(任何对象)存储到文件中,因此您只需加载和使用它们即可.

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

(另一个选项是 json 模块,该模块序列化为json.以类似的方式使用,但只能保存字典,列表,字符串和整数)

(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)

这篇关于用Python存储数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆