我一直用搁架以这种方式存放大量物品:
字典,字符串作为键,列表作为值:
即
data["MITL"] = ["Radio And Television Broadcasting And Communications Equipment", "Communication Equipment"]
或更简洁:
...
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>
我从这个.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>
据我所知,该文件写得很好,它正在检索我的问题数据。
From the documentation:“数据库(不幸的是)也受到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])
和错误/输出:
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
我已经看到其他人遇到导致相同错误的问题,但是他们在7.4.1之前使用的是Python版本,我认为他们的错误原因不同。 Python shelve module question
那么,我的问题:
dbm有什么限制?
有没有办法解决搁置的大对象(包含大型列表的字典作为值)?
如果没有,如果我不想将其保存在RAM中,那么存储数据的更好方法是什么? (这是我认为使用Shelve的目的)