使用Klepto保存和编辑Python [英] Python Saving and Editing with Klepto

查看:196
本文介绍了使用Klepto保存和编辑Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我的问题很具体,我事先表示歉意.我是一名新程序员,并尝试从头开始自己开发.我能看到的最后一个问题是相对成功的.您可以在此处完整查看我的代码.

Okay so my question is pretty specific and I apologize in advance. I'm a new programmer and tried developing on my own from scratch. It was relatively successful only I have one last problem, that I can see. You can view my code here in its entirety.

项目

所以我遇到的问题与保存文件的方式有关.我首先尝试将其腌制,因为它是一本字典,但是由于我的字典是
,所以我一直遇到错误 (名称,类别)对.

So the problem I'm having is related to the way I save the file. I first tried to pickle it since it's a dictionary but I kept getting an error because my dictionary is
(name, class) pairs.

我在这里搜索了一下,发现我可以尝试JSON来做同样的事情.最终出现同样的错误.最终,我找到了有效的klepto模块.我成功保存了字典并成功加载了字典.直到后来,我发现可以将新项目添加到文件中,但是每当我从字典中删除某些内容并保存时.下次我加载它.我删除的钥匙仍然在那里.

I searched around on here and saw I could try JSON to do the same. Ended up with the same kind of errors. Eventually I found the klepto module which worked. I saved my dictionary successfully and loaded it successfully. It wasn't until later I found that I can add new items to the file but whenever I remove something from my dict and save. The next time I load it. The key I removed is still there.

TLDR:可以在dict上添加一些内容,然后保存到txt文件,但是当我从dict中删除并保存它时,不会保存删除的键.

TLDR: Can add things just fine to my dict and save to txt file but when I remove from the dict and save it won't save the removed key.

无论如何,我都对问题的根源感到困惑,那就是保存文件的方式或加载文件的方式,还是两者兼而有之?任何帮助将不胜感激.

Anyway I'm stumped on where my problem lies in the way I'm saving the file or the way I'm loading it or both? Any help would be very appreciated.

好的,我假设这是当前设置保存和加载的方式.

Ok I'm assuming it's the way I have it currently set to save and load.

try:
    alcohols = file_archive('Alcohols.txt')
    alcohols.load()
except IOError:
    alcohols = {}
    print('alcohols doesn\'t exist.')

print('Exiting and saving the data.')
        alcohols.dump('Alcohols.txt') #saves the dictionary data as is

在添加新项目时,它可以很好地保存字典,但要说我必须进行编辑并删除某些内容,然后保存并退出.下次加载时,它会包含旧项目以及任何新项目.奇怪的是,我似乎在所有编辑中都破坏了某些内容.不保存新条目.

It saves the dictionary fine while adding new items but say I have to make an edit and remove something then save and exit. Next time loaded up it would have the old items as well as any new ones. Oddly enough I seem to have broken something in all my editing. Doesn't save new entries.

Edit2:

                del alcohols[name] #deletes the key out of the dict

这是我卸下钥匙的方式.最初,我使用的是pop方法,但是当它无法保存更改时,我尝试了此方法.值得注意的是,它DID从字典中删除了它们的键,值,但是保存和重新加载不会反映该更改.

This is how I remove the keys. Originally I was using the pop method but when it wouldn't save the changes I tried this. As a note it DID remove they key, value from the dictionary but saving and reloading wouldn't reflect that change.

                alcohols[name] = Alcohol() #instantiates the new class

这就是我创建新的键,值对的方式.

Ths is how I am creating new key, value pairs.

已解决的修改:

我的问题是我从字典中删除它们的方式.万一以后有人偶然发现这里.看看@Mike Mckerns的答案.必须从已存档的字典中删除.

My problem was with the way I deleted them from the dictionary. In case anyone stumbles into here later. Look at @Mike Mckerns answer. Had to remove from the archived dictionary.

推荐答案

基本上,您是从内存中"缓存中删除,而不是从文件"缓存中删除.默认情况下,klepto存档为您提供内存中"缓存,您可以直接通过dict接口使用它,还为您提供一个archive后端.

Basically, you are deleting from the "in-memory" cache, and not from the "file" cache. A klepto archive by default gives you "in-memory" cache, which you use directly through the dict interface, and it also gives you an archive which is the back-end.

因此,当您使用dump时,会将内存中的项目传输到后端. 要从缓存和存档中删除,您必须同时从两者中删除.

Thus, when you dump, you transfer the in-memory items to the back-end. To delete from the cache and from the archive, you have to delete from both.

>>> from klepto.archives import *
>>> arch = file_archive('foo.txt')
>>> arch['a'] = 1
>>> arch['b'] = 2
>>> # look at the "in-memory" copy
>>> arch
file_archive('foo.txt', {'a': 1, 'b': 2}, cached=True)
>>> # look at the "on-disk" copy
>>> arch.archive
file_archive('foo.txt', {}, cached=False)
>>> # dump from memory to the file
>>> arch.dump()
>>> arch.archive
file_archive('foo.txt', {'a': 1, 'b': 2}, cached=False)
>>> arch 
file_archive('foo.txt', {'a': 1, 'b': 2}, cached=True)
>>> # delete from the in-memory cache
>>> arch.pop('a')
1
>>> # delete from the on-disk cache
>>> arch.archive.pop('a')
1
>>> arch
file_archive('foo.txt', {'b': 2}, cached=True)
>>> arch.archive
file_archive('foo.txt', {'b': 2}, cached=False)

我想我可以通过一个函数调用将两者删除都变得更容易...

I guess I could make it easier to delete from both in a single function call...

这篇关于使用Klepto保存和编辑Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆