Abh*_*yan 9 python shelve python-3.4
我正在使用Pycharm.首先,每当在Pycharm中导入任何模块时.完整的进口线淡出.但是如果import shelve没有淡出.此外,当我运行该文件时,我得到以下错误:
Traceback (most recent call last):
File "/Users/abhimanyuaryan/PycharmProjects/shelve/main.py", line 13, in <module>
s = shelve.open("file.dat")
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/shelve.py", line 239, in open
return DbfilenameShelf(filename, flag, protocol, writeback)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/shelve.py", line 223, in __init__
Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/dbm/__init__.py", line 88, in open
raise error[0]("db type could not be determined")
dbm.error: db type could not be determined
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import shelve
s = shelve.open("file.dat")
s["first"] = (1182, 234, 632, 4560)
s["second"] = {"404": "file is not present", "googling": "Google to search your content"}
s[3] = ["abhilasha", "jyoti", "nirmal"]
s.sync()
print(s["first"])
print(s["second"])
print(s[3])
Run Code Online (Sandbox Code Playgroud)
OP在'file.dat'由pickle- 创建的评论中解释- 这就是问题所在! pickle 不使用任何数据库格式 - 它使用自己的!创建file.dat与shelve在第一时间(即运行shelve时file.dat不存在与东西保存到它),你会被罚款.
OP评论说:"在这种情况下,我仍然没有得到什么问题".答:这个问题是pickle不会没有任何的DB格式创建一个文件,shelve可以使用.使用单个模块进行序列化和反序列化 - 或者只是pickle,或者只是shelve- 它会更好地工作:-).
| 归档时间: |
|
| 查看次数: |
7146 次 |
| 最近记录: |