我需要存储他们购买的客户和汽车的基本数据以及这些汽车的付款时间表。这些数据来自用Python编写的GUI。我没有足够的经验来使用像sql这样的数据库系统,所以我想将我的数据作为纯文本存储在文件中。它不必上网。使用Python的基本数据存储
为了能够搜索和过滤它们,首先我将数据(列表列表)转换为字符串,然后当我需要将数据重新转换为常规Python列表语法时。我知道这是一种非常暴力的方式,但是这样做还是安全的,或者你能以另一种方式给我建议吗?
我需要存储他们购买的客户和汽车的基本数据以及这些汽车的付款时间表。这些数据来自用Python编写的GUI。我没有足够的经验来使用像sql这样的数据库系统,所以我想将我的数据作为纯文本存储在文件中。它不必上网。使用Python的基本数据存储
为了能够搜索和过滤它们,首先我将数据(列表列表)转换为字符串,然后当我需要将数据重新转换为常规Python列表语法时。我知道这是一种非常暴力的方式,但是这样做还是安全的,或者你能以另一种方式给我建议吗?
这是绝对不安全的保存您的数据库以文本格式(或者使用咸菜或其他)。存储数据时出现问题可能会导致损坏。更不用说数据被盗用的风险。
随着数据集的增长,可能会出现性能问题。
看看sqlite(或sqlite3),它比mysql更小,更容易管理。除非你有一个适合文本文件的非常小的数据集。
P/S:顺便说一句,在使用Python的Berkeley DB简单,你不必去学习所有的DB的事情,只需要导入bsddb
您可以使用此lib添加到对象写入文件http://docs.python.org/library/pickle.html
使用泡菜是不错的答案,但我个人更喜欢搁置。它允许你将变量保持在启动之间的相同状态,我发现它比直接使用pickle容易。 http://docs.python.org/library/shelve.html
将数据写入文件不是数据存储的安全方式。更好地使用像sqlalchemy这样简单的数据库库。这是一个简单的数据库使用ORM ...
您还可以保持简单的数据在纯文本文件。然后,你没有太多的支持,但是,检查数据的一致性,双值等
这是我的简单'卡文件'类型数据文本文件code snippet使用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
"""
我与其他人严肃和重要的数据将是某种类型的光的数据库更安全,但也能感受到同情希望让事情变得简单和透明的同意。
因此,而不是你自己发明的基于文本的数据格式,我建议你使用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中,文档将帮助你。
至少有另一种选择要考虑:)
非常简单和基本的 - (更多@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})
'bsddb'模块已经从Python中移除3图书馆。 (它可能仍然是第三方) – 2014-07-09 19:44:54