使用 Python 进行基本数据存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3276230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:22:22  来源:igfitidea点击:

Basic data storage with Python

pythondatabasedata-storage

提问by erkangur

I need to store basic data of customer's and cars that they bought and payment schedule of these cars. These data come from GUI, written in Python. I don't have enough experience to use a database system like sql, so I want to store my data in a file as plain text. And it doesn't have to be online.

我需要存储他们购买的客户和汽车的基本数据以及这些汽车的付款时间表。这些数据来自用 Python 编写的 GUI。我没有足够的经验来使用像 sql 这样的数据库系统,所以我想将我的数据作为纯文本存储在一个文件中。而且它不必在线。

To be able to search and filter them, first I convert my data (lists of lists) to the string then when I need the data re-convert to the regular Python list syntax. I know it is a very brute-force way, but is it safe to do like that or can you advice me to another way?

为了能够搜索和过滤它们,首先我将我的数据(列表列表)转换为字符串,然后当我需要将数据重新转换为常规 Python 列表语法时。我知道这是一种非常暴力的方式,但是这样做是否安全,或者您可以建议我采用另一种方式吗?

采纳答案by ntcong

It is never safe to save your database in a text format (or using pickle or whatever). There is a risk that problems while saving the data may cause corruption. Not to mention risks with your data being stolen.

以文本格式(或使用 pickle 或其他格式)保存数据库永远是不安全的。保存数据时出现问题可能会导致损坏。更不用说您的数据被盗的风险了。

As your dataset grows there may be a performance hit.

随着数据集的增长,性能可能会受到影响。

have a look at sqlite (or sqlite3) which is small and easier to manage than mysql. Unless you have a very small dataset that will fit in a text file.

看看sqlite(或sqlite3),它比mysql更小且更易于管理。除非您有一个非常小的数据集可以放入文本文件。

P/S: btw, using berkeley db in python is simple, and you don't have to learn all the DB things, just import bsddb

P/S:顺便说一句,在python中使用berkeley db很简单,而且你不必学习所有的DB东西,只需导入bsddb

回答by Quonux

You can use this lib to write an object into a file http://docs.python.org/library/pickle.html

您可以使用此库将对象写入文件http://docs.python.org/library/pickle.html

回答by g.d.d.c

The answer to use pickle is good, but I personally prefer shelve. It allows you to keep variables in the same state they were in between launches and I find it easier to use than pickle directly. http://docs.python.org/library/shelve.html

使用泡菜的答案很好,但我个人更喜欢搁置。它允许您将变量保持在启动之间的相同状态,我发现它比直接使用 pickle 更容易使用。 http://docs.python.org/library/shelve.html

回答by svenwltr

Writing data in a file isn't a safe way for datastorage. Better use a simple database libary like sqlalchemy. It is a ORM for easy database usage...

将数据写入文件并不是一种安全的数据存储方式。最好使用一个简单的数据库libary像SQLAlchemy的。它是一个便于数据库使用的 ORM...

回答by Tony Veijalainen

You can also keep simple data in plain text file. Then you have not much support, however, to check consistency of data, double values etc.

您还可以将简单数据保存在纯文本文件中。但是,您没有太多支持来检查数据的一致性,双值等。

Here is my simple 'card file' type data in text file code snippetusing namedtuple so that you can access values not only by index in line but by they header name:

这是我使用 namedtuple 的文本文件代码片段中的简单“卡片文件”类型数据,这样您不仅可以通过行中的索引访问值,还可以通过它们的标题名称访问值:

# text based data input with data accessible
# with named fields or indexing
from __future__ import print_function ## Python 3 style printing
from collections import namedtuple
import string

filein = open("sample.dat")

datadict = {}

headerline = filein.readline().lower() ## lowercase field names Python style
## first non-letter and non-number is taken to be the separator
separator = headerline.strip(string.lowercase + string.digits)[0]
print("Separator is '%s'" % separator)

headerline = [field.strip() for field in headerline.split(separator)]
Dataline = namedtuple('Dataline',headerline)
print ('Fields are:',Dataline._fields,'\n')

for data in filein:
    data = [f.strip() for f in data.split(separator)]
    d = Dataline(*data)
    datadict[d.id] = d ## do hash of id values for fast lookup (key field)

## examples based on sample.dat file example
key = '123'
print('Email of record with key %s by field name is: %s' %
      (key, datadict[key].email))

## by number
print('Address of record with key %s by field number is: %s' %
      (key ,datadict[key][3]))

## print the dictionary in separate lines for clarity
for key,value in  datadict.items():
    print('%s: %s' % (key, value))

input('Ready') ## let the output be seen when run directly

""" Output:
Separator is ';'
Fields are: ('id', 'name', 'email', 'homeaddress') 

Email of record with key 123 by field name is: [email protected]
Address of record with key 123 by field number is: 456 happy st.
345: Dataline(id='345', name='tony', email='[email protected]', homeaddress='Espoo Finland')
123: Dataline(id='123', name='gishi', email='[email protected]', homeaddress='456 happy st.')
Ready
"""

回答by Hagge

I agree with the others that serious and important data would be more secure in some type of light database but can also feel sympathy for the wish to keep things simple and transparent.

我同意其他人的看法,即严肃和重要的数据在某种类型的轻型数据库中会更安全,但也对保持简单和透明的愿望感到同情。

So, instead of inventing your own text-based data-format I would suggest you use YAML

因此,与其发明您自己的基于文本的数据格式,我建议您使用YAML

The format is human-readable for example:

该格式是人类可读的,例如:

List of things:
    - Alice
    - Bob
    - Evan

You load the file like this:

你像这样加载文件:

>>> import yaml
>>> file = open('test.yaml', 'r')
>>> list = yaml.load(file)

And list will look like this:

列表将如下所示:

{'List of things': ['Alice', 'Bob', 'Evan']}

Of course you can do the reverse too and save data into YAML, the docs will help you with that.

当然,您也可以反向操作并将数据保存到 YAML 中,文档将帮助您做到这一点。

At least another alternative to consider :)

至少要考虑另一种选择:)

回答by DirectorX

very simple and basic - (more @ http://pastebin.com/A12w9SVd)

非常简单和基本 - (更多 @ http://pastebin.com/A12w9SVd

import json, os

db_name = 'udb.db'

def check_db(name = db_name):
    if not os.path.isfile(name):
        print 'no db\ncreating..'
        udb = open(db_name,'w')
        udb.close()

def read_db():
    try:
        udb = open(db_name, "r")
    except:
        check_db()
        read_db()
    try:
        dicT = json.load(udb)
        udb.close()
        return dicT
    except:
        return {}    

def update_db(newdata):
    data = read_db()
    wdb = dict(data.items() + newdata.items())    
    udb = open(db_name, 'w')
    json.dump(wdb, udb)
    udb.close()

using:

使用:

def adduser():
    print 'add user:'
    name = raw_input('name > ')
    password = raw_input('password > ')

    update_db({name:password})