PyN*_*bie 1 python serialization
我正在使用一组数据,我已将其转换为字典列表
例如,我列表中的一个项目是
{'reportDate': u'R20070501', 'idnum': u'1078099', 'columnLabel': u'2005',
'actionDate': u'C20070627', 'data': u'76,000', 'rowLabel': u'Sales of Bananas'}
Run Code Online (Sandbox Code Playgroud)
按要求
我列表中的第二项可能是:
{'reportDate': u'R20070501', 'idnum': u'1078099', 'columnLabel': u'2006',
'actionDate': u'C20070627', 'data': u'86,000', 'rowLabel': u'Sales of Bananas'}
Run Code Online (Sandbox Code Playgroud)
第三项可能是:
{'reportDate': u'R20070501', 'idnum': u'1078100', 'columnLabel': u'Full Year 2005',
'actionDate': u'C20070627', 'data': u'116,000', 'rowLabel': u'Sales of Cherries'}
Run Code Online (Sandbox Code Playgroud)
第四项可能是:
{'reportDate': u'R20070501', 'idnum': u'1078100', 'columnLabel': u'Full Year 2006',
'actionDate': u'C20070627', 'data': u'76,000', 'rowLabel': u'Sales of Sales of Cherries'}
Run Code Online (Sandbox Code Playgroud)
我需要腌制这个的原因是因为在合并结果并将它们放入数据库之前,我需要找出列标记的所有方法.第一个和第二个项目将是结果中的一行,第三个和第四个项目将是结果中的下一行(在有人决定统一列标题标签应该是什么之后)
我测试了泡菜,并能够保存和检索我的数据.但是,我需要能够保留输出中的顺序.我有一个想法是添加另一个可以作为计数器的密钥,这样我就可以检索我的数据,然后按计数器排序.有没有更好的办法?
我不想把它放到数据库中,因为它不是永久性的.
我在下面标出了答案.这不是我得到的,所以我需要弄清楚问题是否在我的代码中的其他地方.
那泡菜怎么了?如果您将数据结构化为一个dicts列表,那么一切都应该按照您的意愿工作(如果我理解您的问题).
>>> import pickle
>>> d1 = {1:'one', 2:'two', 3:'three'}
>>> d2 = {1:'eleven', 2:'twelve', 3:'thirteen'}
>>> d3 = {1:'twenty-one', 2:'twenty-two', 3:'twenty-three'}
>>> data = [d1, d2, d3]
>>> out = open('data.pickle', 'wb')
>>> pickle.dump(data, out)
>>> out.close()
>>> input = open('data.pickle')
>>> data2 = pickle.load(input)
>>> data == data2
True
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
189 次 |
| 最近记录: |