gpt4 book ai didi

python - python 的搁置模块是否有最大尺寸?

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:06 26 4
gpt4 key购买 nike

我在尝试打开超过一定大小(实际上很小(< 1MB))的搁置持久文件时遇到此异常,但我不确定确切的数字在哪里。现在,我知道 pickle 有点像 python 的私生子,而 shelve 并不被认为是一个特别强大的解决方案,但它恰好很好地解决了我的问题(理论上)而且我一直无法找到原因这个异常(exception)。

Traceback (most recent call last):
File "test_shelve.py", line 27, in <module>
print len(f.keys())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shelve.py", line 101, in keys
return self.dict.keys()
SystemError: Negative size passed to PyString_FromStringAndSize

我可以一致地复制它,但我在谷歌上找不到太多。这是一个将重现的脚本。

import shelve
import random
import string
import pprint

f = shelve.open('test')
# f = {}

def rand_list(list_size=20, str_size=40):
return [''.join([random.choice(string.ascii_uppercase + string.digits) for j in range(str_size)]) for i in range(list_size)]

def recursive_dict(depth=3):
if depth==0:
return rand_list()
else:
d = {}
for k in rand_list():
d[k] = recursive_dict(depth-1)
return d

for k,v in recursive_dict(2).iteritems():
f[k] = v

f.close()

f = shelve.open('test')
print len(f.keys())

最佳答案

关于错误本身:

The idea circulating on the web is the data size exceeded the largest integer possible on that machine (the largest 32 bit (signed) integer is 2 147 483 647), interpreted as a negative size by Python.

您的代码使用 2.7.3 运行,因此可能是一个已修复的错误。

关于python - python 的搁置模块是否有最大尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16326793/

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