gpt4 book ai didi

python-3.x - 错误我不明白

转载 作者:行者123 更新时间:2023-12-03 09:15:09 24 4
gpt4 key购买 nike

我在Python 3.5中产生了此错误:

Traceback (most recent call last): File "C:\Users\Owner\AppData\Local\Programs\Python\Python35\lib\shelve.py", line 111, in __getitem__ value = self.cache[key] KeyError: 'P4_vegetables'



在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last): File "C:\Users\Owner\Documents\Python\Allotment\allotment.py", line 217, in main_program() File "C:\Users\Owner\Documents\Python\Allotment\allotment.py", line 195, in main_program main_program() File "C:\Users\Owner\Documents\Python\Allotment\allotment.py", line 49, in main_program print("Plot 4 - ", s["P4_vegetables"]) File "C:\Users\Owner\AppData\Local\Programs\Python\Python35\lib\shelve.py", line 113, in __getitem__ f = BytesIO(self.dict[key.encode(self.keyencoding)]) File "C:\Users\Owner\AppData\Local\Programs\Python\Python35\lib\dbm\dumb.py", line 141, in __getitem__ pos, siz = self._index[key] # may raise KeyError KeyError: b'P4_vegetables'

最佳答案

已经有一段时间了,但万一有人遇到此情况:以下错误

Traceback (most recent call last):
File "filepath", line 111, in __getitem__
value = self.cache[key]
KeyError: 'item1'

如果尝试检索 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.

希望这对遇到与原始海报类似问题的人有所帮助。

关于python-3.x - 错误我不明白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37146842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com