如何在货架上取整数键? [英] How do I take integer keys in shelve?
问题描述
追溯(最近的最后一次呼叫):
文件./write.py,第12行,
data [id] = {Id:id,Name:name}
文件/usr/lib/python2.5/shelve .py,第124行,__setitem__
self.dict [key] = f.getvalue()
文件/usr/lib/python2.5/bsddb/__init__.py,第230行,在__setitem__
_DeadlockWrap(wrapF)#self.db [key] = value
文件/usr/lib/python2.5/bsddb/dbutils.py,第62行在DeadlockWrap
return函数(* _ args,** _ kwargs)
文件/usr/lib/python2.5/bsddb/__init__.py,第229行,wrapF
self.db [key] = value
TypeError:仅允许使用Recno和Queue DB的
我的代码:
#!/ usr / bin / python
import shelve
data = shelve。 open(data.txt)
ans ='y'
while ans ==y:
id = input(Enter Id:)
name = raw_input(Enter name:)
data [id] = {Id:id,Name:name}
ans = raw_input你想继续(y / n)? :)
data.close()
我的程序或搁置不支持整数键?
编辑1:
在程序中,我试图将Id和Name的字典存储在另一个字典中,其中Id为密钥,然后尝试将其存储在文件中。
我需要使用Recno或Queue DB和搁架吗?我是一个初学者,事情令人困惑。
让我知道我不清楚我的问题。
谢谢。
货架模块使用底层数据库包(如dbm,gdbm或bsddb)。
架子是一个持久的类字典,与 dbm数据库是一个架子中的值(而不是键!)可以是基本上任意的Python对象 - 腌泡模块可以处理的任何东西,包括大多数类实例,递归e数据类型和包含大量共享子对象的对象。钥匙是普通字符串。 示例部分为您提供了证明。
这应该工作。以下是我在代码中所做的工作 -
import shelve
#Create shelve
s = shelve.open('test_shelf.db')
try:
s ['key1'] = {'int':10,'float':9.5,'string':'Sample data'}
finally:
s.close()
#Access shelve
s = shelve.open('test_shelf.db')
try:
existing = s ['key1']
finally:
s.close()
打印现有
更新:您可以尝试 pickle
模块。它不是键值数据库,但您可以随时将其数据结构作为键值对构建,然后将其发送到 pickle
-
如果你有一个对象x和一个被打开来写入的文件对象f,那么最简单的腌制对象只需一行代码
pickle.dump(x,f)
如果f是已被打开读取的文件对象,则再次取消对象:
x = pickle.load(f)
我听到 cPickle
比泡菜
。你可以尝试这个,如果你有很多的数据要存储。
I want to store an integer key in shelve. But when I try to store integer key in shelve it give me an error
Traceback (most recent call last): File "./write.py", line 12, in data[id] = {"Id": id, "Name": name} File "/usr/lib/python2.5/shelve.py", line 124, in __setitem__ self.dict[key] = f.getvalue() File "/usr/lib/python2.5/bsddb/__init__.py", line 230, in __setitem__ _DeadlockWrap(wrapF) # self.db[key] = value File "/usr/lib/python2.5/bsddb/dbutils.py", line 62, in DeadlockWrap return function(*_args, **_kwargs) File "/usr/lib/python2.5/bsddb/__init__.py", line 229, in wrapF self.db[key] = value TypeError: Integer keys only allowed for Recno and Queue DB's
My Code :
#!/usr/bin/python
import shelve
data = shelve.open("data.txt")
ans = 'y'
while ans == "y":
id = input("Enter Id : ")
name = raw_input("Enter name : ")
data[id] = {"Id": id, "Name": name}
ans = raw_input("Do you want to continue (y/n) ? : ")
data.close()
Is something wrong in my program or shelve does not supports integer keys at all ?
Edit 1 :
In program I am trying to store a dictionary of Id and Name inside another dictionary with Id as a key. And then trying to store it in a file.
Do I need to use Recno or Queue DB's along with shelve? I am a beginner and things are confusing.
Let me know if I am not clear with my question.
Thanks.
The shelve module uses an underlying database package (such as dbm, gdbm or bsddb) .
A "shelf" is a persistent, dictionary-like object. The difference with "dbm" databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects -- anything that the pickle module can handle. This includes most class instances, recursive data types, and objects containing lots of shared sub-objects. The keys are ordinary strings. The examples section gives you the proof.
This should work. Here's what I do in my code -
import shelve
#Create shelve
s = shelve.open('test_shelf.db')
try:
s['key1'] = { 'int': 10, 'float':9.5, 'string':'Sample data' }
finally:
s.close()
#Access shelve
s = shelve.open('test_shelf.db')
try:
existing = s['key1']
finally:
s.close()
print existing
UPDATE: You could try pickle
module. It is not a key-value database but you can always build your data structure as a key-value pairs and then send it to pickle
-
If you have an object x, and a file object f that's been opened for writing, the simplest way to pickle the object takes only one line of code
pickle.dump(x, f)
To unpickle the object again, if f is a file object which has been opened for reading:
x = pickle.load(f)
I hear cPickle
is a lot faster than pickle
. You can try this if you have lot of data to store.
这篇关于如何在货架上取整数键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!