使用 Python 进行基本数据存储

发布于 2024-09-10 01:24:59 字数 227 浏览 5 评论 0原文

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

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不知在何时 2024-09-17 01:24:59

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

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

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

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

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.

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.

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

隔纱相望 2024-09-17 01:24:59

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

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

冷情 2024-09-17 01:24:59

我同意其他人的观点,即严肃而重要的数据在某种类型的轻型数据库中会更安全,但也可以同情保持事情简单和透明的愿望。

因此,我建议您不要发明自己的基于文本的数据格式,而是使用 YAML

该格式是人类可读的示例:

List of things:
    - Alice
    - Bob
    - Evan

您像这样加载文件:

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

列表将如下所示:

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

当然您也可以执行相反的操作并将数据保存到 YAML 中,文档将帮助您完成此操作。

至少可以考虑另一种选择:)

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

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.

At least another alternative to consider :)

小情绪 2024-09-17 01:24:59

非常简单和基本 - (更多@ 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()

使用:

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

    update_db({name:password})

very simple and basic - (more @ 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})
十六岁半 2024-09-17 01:24:59

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

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

宣告ˉ结束 2024-09-17 01:24:59

将数据写入文件并不是一种安全的数据存储方式。最好使用简单的数据库库,例如 sqlalchemy。它是一个易于数据库使用的 ORM...

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...

眼藏柔 2024-09-17 01:24:59

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

这是文本文件中我的简单“卡片文件”类型数据 代码片段使用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
"""

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 snippet using namedtuple so that you can access values not only by index in line but by they header name:

# 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
"""
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文