将字典保存在文件中 [英] save a dictionary in a file

查看:65
本文介绍了将字典保存在文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-----开始PGP签名消息-----

哈希:SHA1





我的程序构建了一个字典,我想保存在一个文件中。


我的问题是简单的方法是什么?

我想到的第一个解决方案是将字典转换为

列表,其中键是该列表中的第一个元素,值为

作为第二个。然后我可以用pickle来写和读

操作。这看起来简单易行。


有没有更常用的表格,但是用一种简单的方式?


Luis

----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.2.4(GNU / Linux)

评论:使用GnuPG使用Thunderbird - http://enigmail.mozdev.org


iD8DBQFBmPRdHn4UHCY8rB8RApRjAKCF4 / pEMbQQ10Quf5CZO / nTL2CpHgCfVWZF

G36HuPUbLXC1qtB + DP8BhK0 =

= f + vS

----- END PGP SIGNATURE- ----

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

my program builds a dictionary that I would like to save in a file.

My question is what are the simple ways to do it?

The first solution I''ve thought of is to transform the dictionary in a
list, with the key being the first element on that list, and the value
as the second. Then I could use pickle to do the write and read
operations. It seems simple and easy.

Is there a more commonly used form to do it, yet in a simple way?

Luis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBmPRdHn4UHCY8rB8RApRjAKCF4/pEMbQQ10Quf5CZO/nTL2CpHgCfVWZF
G36HuPUbLXC1qtB+DP8BhK0=
=f+vS
-----END PGP SIGNATURE-----

推荐答案

Luis P. Mendes写道:
Luis P. Mendes wrote:


我的程序构建一个字典,我想保存在一个文件中。

我的问题是什么是简单的方法呢?

我想到的第一个解决方案是将字典转换为
列表中的字典,其中键是该列表中的第一个元素,值为
作为第二个元素。然后我可以使用pickle进行写入和读取操作。看起来简单易行。

是否有一种更常用的形式可以做到这一点,但是以一种简单的方式?

Luis
Hi,

my program builds a dictionary that I would like to save in a file.

My question is what are the simple ways to do it?

The first solution I''ve thought of is to transform the dictionary in a
list, with the key being the first element on that list, and the value
as the second. Then I could use pickle to do the write and read
operations. It seems simple and easy.

Is there a more commonly used form to do it, yet in a simple way?

Luis




查看pickle或ConfigParser模块。

这两个模块都可以用不同的方式完成你想要的工作。


Eric



Look at the pickle or ConfigParser modules.
Both of these will do what you are looking for in different ways.

Eric


Luis P. Mendes写道:
Luis P. Mendes wrote:
哈希:SHA1


<我的程序构建了一个我想保存在文件中的字典。

我的问题是什么是简单的方法呢?
Hash: SHA1

Hi,

my program builds a dictionary that I would like to save in a file.

My question is what are the simple ways to do it?




我们为SiGeFi实现了这个(sf.net/projects/sigefi/)


来自config.py


类PersistentDict( dict):

''''''持久词典。


这是一个将数据保存并加载到文件中的字典。

'''''


def __init __(self,nomarchivo):

''''''创建一个PersistentDict inst ance。


收到文件名,但如果文件不存在(还)

开始工作

空数据集并将在保存数据时创建文件。


:参数:

- `nomarchivo`:file保存数据的地方。

'''''

self._nomarchivo = nomarchivo

dict .__ init __(self)


如果不是os.access(nomarchivo,os.W_OK):

返回


arch = open(nomarchivo,' 'r'')

olddict = cPickle.load(arch)

arch.close()

self.update(olddict)

返回


def save(self):

''''''将数据保存到磁盘。''''' '

arch = open(self._nomarchivo,''w'')

cPickle.dump(self,arch)

arch.close ()

返回


问候,


-

Mariano



We implemented this for SiGeFi (sf.net/projects/sigefi/)

From config.py

class PersistentDict(dict):
''''''Persistent dictionary.

It''s a dictionary that saves and loads its data to a file.
''''''

def __init__(self, nomarchivo):
''''''Creates a PersistentDict instance.

Receives a filename, but if the file does not exist (yet)
starts to work
with an empty data set and will create the file when the data
is saved.

:Parameters:
- `nomarchivo`: file where the data is saved.
''''''
self._nomarchivo = nomarchivo
dict.__init__(self)

if not os.access(nomarchivo, os.W_OK):
return

arch = open(nomarchivo, ''r'')
olddict = cPickle.load(arch)
arch.close()
self.update(olddict)
return

def save(self):
''''''Saves the data to disk.''''''
arch = open(self._nomarchivo, ''w'')
cPickle.dump(self, arch)
arch.close()
return

Regards,

--
Mariano


也许我错了唱一些东西,但为什么你不只是腌制字典

本身而不是你把它变成一个列表并腌制列表?我不知道在腌制字典时有什么限制,我在

我的一些代码中这样做。我实际上是在腌制嵌套词典的结构(即,顶级词典中的
值本身就是字典,其中一些值是b b B值,依此类推)。 />

Dan


" Luis P. Mendes" <卢************ @ netvisaoXX.pt>在消息中写道

新闻:2v ************* @ uni-berlin.de ...
Maybe I am missing something, but why don''t you just pickle the dictionary
itself and instead you transform it into a list and pickle the list? I
don''t know of any restrictions in pickling a dictionary and I do that in
some of my code. I actually pickle a structure of nested dictionaries (ie,
values in the top dictionary are dictionaries themselves, some of their
values are dictionaries, and so on).

Dan

"Luis P. Mendes" <lu************@netvisaoXX.pt> wrote in message
news:2v*************@uni-berlin.de...
----- BEGIN PGP签名消息-----
哈希:SHA1


我的程序构建了一个字典,我想保存在一个文件中。 />
我的问题是什么是简单的方法呢?

我想到的第一个解决方案是将字典转换成
列表, key是该列表中的第一个元素,值为
作为第二个元素。然后我可以使用pickle进行写入和读取操作。看起来简单易行。

是否有一种更常用的形式来实现它,但是以一种简单的方式?

Luis
----- BEGIN PGP SIGNATURE -----
版本:GnuPG v1.2.4(GNU / Linux)
评论:使用GnuPG和Thunderbird - http://enigmail.mozdev.org

iD8DBQFBmPRdHn4UHCY8rB8RApRjAKCF4 / pEMbQQ10Quf5CZO / nTL2CpHgCfVWZF
G36HuPUbLXC1qtB + DP8BhK0 =
= f + vS
-----结束PGP签名-----
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

my program builds a dictionary that I would like to save in a file.

My question is what are the simple ways to do it?

The first solution I''ve thought of is to transform the dictionary in a
list, with the key being the first element on that list, and the value
as the second. Then I could use pickle to do the write and read
operations. It seems simple and easy.

Is there a more commonly used form to do it, yet in a simple way?

Luis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBmPRdHn4UHCY8rB8RApRjAKCF4/pEMbQQ10Quf5CZO/nTL2CpHgCfVWZF
G36HuPUbLXC1qtB+DP8BhK0=
=f+vS
-----END PGP SIGNATURE-----



这篇关于将字典保存在文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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