我需要存儲他們購買的客戶和汽車的基本數據以及這些汽車的付款時間表。這些數據來自用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