J.B*_*ker -4 error-handling python-3.x
我在 Python 3.5 中产生了这个错误:
回溯(最近一次调用):文件“C:\Users\Owner\AppData\Local\Programs\Python\Python35\lib\shelve.py”,第 111 行,在 __getitem__ value = self.cache[key] KeyError: ' P4_蔬菜'
在处理上述异常的过程中,又发生了一个异常:
回溯(最近一次调用):文件“C:\Users\Owner\Documents\Python\Allotment\allotment.py”,第 217 行,在 main_program() 文件“C:\Users\Owner\Documents\Python\Allotment\ allotment.py", line 195, in main_program main_program() 文件 "C:\Users\Owner\Documents\Python\Allotment\allotment.py", line 49, in main_program print("Plot 4 - ", s["P4_vegetables "]) 文件 "C:\Users\Owner\AppData\Local\Programs\Python\Python35\lib\shelve.py",第 113 行,在 __getitem__ f = BytesIO(self.dict[key.encode(self.keyencoding) ]) 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python35\lib\dbm\dumb.py”,第 141 行,在 __getitem__ pos, siz = self._index[key] # 可能会引发 KeyError KeyError : b'P4_vegetables'
已经有一段时间了,但万一有人遇到这个:以下错误
Traceback (most recent call last):
File "filepath", line 111, in __getitem__
value = self.cache[key]
KeyError: 'item1'
Run Code Online (Sandbox Code Playgroud)
如果尝试在with块外检索项目,则可能会发生。一旦我们开始在with块外执行代码,架子就会关闭。因此,在打开架子的with块外的架子上执行的任何操作都将被视为无效操作。例如,
import shelve
with shelve.open('ShelfTest') as item:
item['item1'] = 'item 1'
item['item2'] = 'item 2'
item['item3'] = 'item 3'
item['item4'] = 'item 4'
print(item['item1']) # no error, since shelf file is still open
# If we try print after file is closed
# an error will be thrown
# This is quite common
print(item['item1']) #error. It has been closed, so you can't retrieve it.
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助遇到与原始海报类似问题的任何人。
| 归档时间: |
|
| 查看次数: |
1947 次 |
| 最近记录: |