搁置库对python中对象的限制

brw*_*w59 5 python shelve dbm python-3.x

我一直在以这种方式搁置存储大量对象:

以字符串为键,列表为值的字典:

data["MITL"] = ["Radio And Television Broadcasting And Communications Equipment", "Communication Equipment"]
Run Code Online (Sandbox Code Playgroud)

或更简洁地:

...
SIXH.L         Machine Tools & Accessories, 
GOPAIST.BO     Steel & Iron, 
HERITGFOO.NS   Food Wholesale, 
MITL           Radio And Television Broadcasting And Communications Equipment, Communication Equipment, 
MMLP           Oil Refining, Marketing, Oil & Gas Pipelines, 
SESL.PA        Diversified Electronics, 
...
<? 30,000 entries>
Run Code Online (Sandbox Code Playgroud)

我从此.db文件中提取并导出到另一个.db文件,因此,行业是关键,列表由股票代号组成。

...
Industrial Electrical Equipment ['PLPC', 'MAG', 'LPTH', 'IIN', 'CUI', 'ULBI', 'APWC', 'CAPC', 'SVT', 'ARTX', 'CPST', 'OSIS', 'LGL', 'BW', 'HPJ', 'AOS', 'FLUX', 'AMSC', 'GTI', 'RTBC', 'AUSI', 'AETI', 'AIMC', 'HYGS', 'BLDP', 'HOLI', 'NPWZ', 'LIME', 'ESNC', 'ZBB', 'CSTU', 'AXPW', 'GBLL', 'EMR', 'BDC', 'BNSO', 'ENS', 'REFR', 'ABAT', 'FELE', 'CYLU', 'XIDEQ', 'LYTS', 'GAI', 'AMOT', 'CUI.V', 'LSCG']
Toy & Hobby Stores ['BBW']
Distribution ['MNST', 'FMX', 'STZ', 'FIZZ', 'BREW', 'THST', 'LBIX', 'ROX', 'COKE', 'KOF', 'PEP', 'COT', 'REED', 'SAM', 'MGPI', 'DPS', 'CCE', 'BORN', 'KO', 'BUD', 'CCU', 'WVVIP', 'TAP', 'WVVI', 'DEO', 'ABEV', 'VCO']
Home Health Care ['AFAM', 'SCAI', 'ADUS', 'AMED', 'LHCG', 'BIOS', 'CHE', 'HASC']
...
<? 300 entries>
Run Code Online (Sandbox Code Playgroud)

据我所知,该文件写的很好,它正在检索作为我问题的数据。

从文档中: “(如果使用的话)数据库也(不幸地)受到dbm的限制-这意味着存储在数据库中的对象(的腌制形式)应该很小,在极少数情况下会发生键冲突可能导致数据库拒绝更新。”

但是,即使有文档,我也找不到有关dbm限制的任何信息。原因一定是因为我存储为值的列表太大。

这是一段代码摘录:

industriesAndTheirStocks = shelve.open("industriesAndTheirStocks")
print(len(industriesAndTheirStocks)) # just to make a point at how many keys there are, proving it's the size of the lists stored that contains the issue

for industry in industriesAndTheirStocks: # fails here because 'industriesAndTheirStocks' can't be iterated through, because it sent a negative number as the size to __iter__
    print("{:<15}".format(industry), end="")
    print(industriesAndTheirStocks[industry])
Run Code Online (Sandbox Code Playgroud)

和错误/输出:

374
Traceback (most recent call last):
  File "read_from_shelve_stock_industry_file.py", line 144, in <module>
    if __name__ == "__main__":main()
  File "read_from_shelve_stock_industry_file.py", line 128, in main
    display_shelve_contents_by_industry()
  File "read_from_shelve_stock_industry_file.py", line 42, in display_shelve_contents_by_industry
    for industry in industriesAndTheirStocks:
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shelve.py", line 95, in __iter__
    for k in self.dict.keys():
SystemError: Negative size passed to PyBytes_FromStringAndSize

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

我已经看到其他人有导致相同错误的问题,但是他们使用的是7.4.1之前的Python版本,我认为他们的错误是由不同的原因引起的。 Python搁置模块问题

所以,我的问题是:

dbm的局限性是什么?

有没有办法解决搁置大对象(包含大列表作为值的字典)的问题?

如果没有,如果我不想将其保存在RAM中,有什么更好的方法来存储数据?(我认为这是使用搁架的目的)