Python论坛  - 讨论区

标题:[python-chinese] 关于用cPickle保存dict的问题

2007年11月14日 星期三 15:25

VespertineR roaning在163.com
星期三 十一月 14 15:25:05 HKT 2007

hi all:
 
我刚开始学Python,遇到了一点小麻烦。
 
想做一个电话簿,需要保存电话信息。使用phonedata.py来保存,主程序是phonebook.py,启动phonebook.py会导入phonebook模块。
phonedata即是一个ab = {}的字典。
phonebook运行完后,用cPackle.dump保存phonedata.ab到phonedata.py时,总是会变成 S"ab = {'1','1'}".
多出来的 S"". 是什么呢,怎么删掉。
谢谢^^
-------------- 涓嬩竴閮ㄥ垎 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20071114/bc7faf88/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月14日 星期三 15:41

Jiahua Huang jhuangjiahua在gmail.com
星期三 十一月 14 15:41:15 HKT 2007

恩,你的话有些让人迷惑,

pickle 并不是用来保存 python 文件的,
他的文件格式并不是 python 代码。

保存::
>>> import cPickle as pickle
>>> ab = {'123':'321'}
>>> pickle.dump(ab, file('/tmp/phonedata.dump', 'w'))

装入::
>>> import cPickle as pickle
>>> ab = pickle.load(file('/tmp/phonedata.dump'))
>>> ab
{'123': '321'}


另外, 字典还可以用 bsddb 或 shelve/zshelve 来保存
保存::
>>> import shelve
>>> ab = shelve.open('/tmp/phonedata.db', writeback=True)
>>> ab['123'] = '321'
>>> ab['13138'] = {'name':'VespertineR', 'email':'roaning at 163.com'}
>>> ab.sync()

装入::
>>> import shelve
>>> ab = shelve.open('/tmp/phonedata.db', writeback=True)
>>> ab
{'123': '321', '13138': {'name': 'VespertineR', 'email': 'roaning at 163.com'}}

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月14日 星期三 15:43

Wayne moonbingbing在gmail.com
星期三 十一月 14 15:43:31 HKT 2007

楼上正解

在07-11-14,Jiahua Huang <jhuangjiahua at gmail.com> 写道:
>
> 恩,你的话有些让人迷惑,
>
> pickle 并不是用来保存 python 文件的,
> 他的文件格式并不是 python 代码。
>
> 保存::
> >>> import cPickle as pickle
> >>> ab = {'123':'321'}
> >>> pickle.dump(ab, file('/tmp/phonedata.dump', 'w'))
>
> 装入::
> >>> import cPickle as pickle
> >>> ab = pickle.load(file('/tmp/phonedata.dump'))
> >>> ab
> {'123': '321'}
>
>
> 另外, 字典还可以用 bsddb 或 shelve/zshelve 来保存
> 保存::
> >>> import shelve
> >>> ab = shelve.open('/tmp/phonedata.db', writeback=True)
> >>> ab['123'] = '321'
> >>> ab['13138'] = {'name':'VespertineR', 'email':'roaning at 163.com'}
> >>> ab.sync()
>
> 装入::
> >>> import shelve
> >>> ab = shelve.open('/tmp/phonedata.db', writeback=True)
> >>> ab
> {'123': '321', '13138': {'name': 'VespertineR', 'email': 'roaning at 163.com
> '}}
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20071114/703d216f/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月14日 星期三 15:44

Jiahua Huang jhuangjiahua在gmail.com
星期三 十一月 14 15:44:51 HKT 2007

pickle 需要你的内存足够大(如果你的电话号码很多)

而 shelve/zshelve 算是磁盘字典,容量不受你内存限制。

另外 shelve.open('/tmp/phonedata.db', writeback=True)
和 zshelve.btopen('/tmp/phonedata.db', writeback=True)
会自动处理回写,不需要你显式 dump 保存。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月14日 星期三 19:04

clfff.peter clfff.peter在gmail.com
星期三 十一月 14 19:04:03 HKT 2007

ÊÕÁË¡£
^_^


ÔÚ07-11-14£¬Jiahua Huang <jhuangjiahua在gmail.com> дµÀ£º
>
> pickle ÐèÒªÄãµÄÄÚ´æ×ã¹»´ó(Èç¹ûÄãµÄµç»°ºÅÂëºÜ¶à)
>
> ¶ø shelve/zshelve ËãÊÇ´ÅÅÌ×ֵ䣬ÈÝÁ¿²»ÊÜÄãÄÚ´æÏÞÖÆ¡£
>
> ÁíÍâ shelve.open('/tmp/phonedata.db', writeback=True)
> ºÍ zshelve.btopen('/tmp/phonedata.db', writeback=True)
> »á×Ô¶¯´¦Àí»Øд£¬²»ÐèÒªÄãÏÔʽ dump ±£´æ¡£
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071114/3f338ae8/attachment-0001.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月14日 星期三 19:33

Cyril.Liu terry6394在gmail.com
星期三 十一月 14 19:33:59 HKT 2007

writeback=True 是不是意味着么次对 dic操作都实时地写道磁盘里呢?!

On Nov 14, 2007 3:44 PM, Jiahua Huang <jhuangjiahua at gmail.com> wrote:

> pickle 需要你的内存足够大(如果你的电话号码很多)
>
> 而 shelve/zshelve 算是磁盘字典,容量不受你内存限制。
>
> 另外 shelve.open('/tmp/phonedata.db', writeback=True)
> 和 zshelve.btopen('/tmp/phonedata.db', writeback=True)
> 会自动处理回写,不需要你显式 dump 保存。
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20071114/38e3ff3a/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月14日 星期三 20:15

Jiahua Huang jhuangjiahua在gmail.com
星期三 十一月 14 20:15:19 HKT 2007

不是。
sync() 或 close() 的时候才显示写磁盘。

不想动笔了,还是直接贴原文吧

    Normally, d[key] returns a COPY of the entry.  This needs care when
    mutable entries are mutated: for example, if d[key] is a list,
            d[key].append(anitem)
    does NOT modify the entry d[key] itself, as stored in the persistent
    mapping -- it only modifies the copy, which is then immediately
    discarded, so that the append has NO effect whatsoever.  To append an
    item to d[key] in a way that will affect the persistent mapping, use:
            data = d[key]
            data.append(anitem)
            d[key] = data

    To avoid the problem with mutable entries, you may pass the keyword
    argument writeback=True in the call to shelve.open.  When you use:
            d = shelve.open(filename, writeback=True)
    then d keeps a cache of all entries you access, and writes them all back
    to the persistent mapping when you call d.close().  This ensures that
    such usage as d[key].append(anitem) works as intended.

    However, using keyword argument writeback=True may consume vast amount
    of memory for the cache, and it may make d.close() very slow, if you
    access many of d's entries after opening it in this way: d has no way to
    check which of the entries you access are mutable and/or which ones you
    actually mutate, so it must cache, and write back at close, all of the
    entries that you access.  You can call d.sync() to write back all the
    entries in the cache, and empty the cache (d.sync() also synchronizes
    the persistent dictionary on disk, if feasible).


在 07-11-14,Cyril. Liu<terry6394 at gmail.com> 写道:
> writeback=True 是不是意味着么次对 dic操作都实时地写道磁盘里呢?!
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月15日 星期四 10:49

李昆仑 jackyboy在163.com
星期四 十一月 15 10:49:43 HKT 2007

Jiahua Huang,您好!

	python手册里也都建议使用shelve,而不直接使用cPickle或者pickle的

======= 2007-11-14 20:15:47 您在来信中写道:=======

>不是。
>sync() 或 close() 的时候才显示写磁盘。
>
>不想动笔了,还是直接贴原文吧
>
>    Normally, d[key] returns a COPY of the entry.  This needs care when
>    mutable entries are mutated: for example, if d[key] is a list,
>            d[key].append(anitem)
>    does NOT modify the entry d[key] itself, as stored in the persistent
>    mapping -- it only modifies the copy, which is then immediately
>    discarded, so that the append has NO effect whatsoever.  To append an
>    item to d[key] in a way that will affect the persistent mapping, use:
>            data = d[key]
>            data.append(anitem)
>            d[key] = data
>
>    To avoid the problem with mutable entries, you may pass the keyword
>    argument writeback=True in the call to shelve.open.  When you use:
>            d = shelve.open(filename, writeback=True)
>    then d keeps a cache of all entries you access, and writes them all back
>    to the persistent mapping when you call d.close().  This ensures that
>    such usage as d[key].append(anitem) works as intended.
>
>    However, using keyword argument writeback=True may consume vast amount
>    of memory for the cache, and it may make d.close() very slow, if you
>    access many of d's entries after opening it in this way: d has no way to
>    check which of the entries you access are mutable and/or which ones you
>    actually mutate, so it must cache, and write back at close, all of the
>    entries that you access.  You can call d.sync() to write back all the
>    entries in the cache, and empty the cache (d.sync() also synchronizes
>    the persistent dictionary on disk, if feasible).
>
>
>在 07-11-14,Cyril. Liu<terry6394在gmail.com> 写道:
>> writeback=True 是不是意味着么次对 dic操作都实时地写道磁盘里呢?!
>>
>_______________________________________________
>python-chinese
>Post: send python-chinese在lists.python.cn
>Subscribe: send subscribe to python-chinese-request在lists.python.cn
>Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
>Detail Info: http://python.cn/mailman/listinfo/python-chinese

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        李昆仑
        jackyboy在163.com
          2007-11-15


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月15日 星期四 10:56

Jiahua Huang jhuangjiahua在gmail.com
星期四 十一月 15 10:56:54 HKT 2007

shelve 的问题是,他默认用 anydbm, 缺了好些 bsddb 的特性,
而 bsddb.dbshelve 又没有回写,

所以我改了个 zshelve , 加上了压缩支持和 bsddb.btopen

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月16日 星期五 15:01

23号 no.0023在gmail.com
星期五 十一月 16 15:01:18 HKT 2007

yeah, where?


On Thu, Nov 15, 2007 at 10:56:54AM +0800, Jiahua Huang wrote:
> shelve 的问题是,他默认用 anydbm, 缺了好些 bsddb 的特性,
> 而 bsddb.dbshelve 又没有回写,
> 
> 所以我改了个 zshelve , 加上了压缩支持和 bsddb.btopen
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-- 
Best Regards,
       No.23
----
My Blog: http://blog.chinaunix.net/u1/42287/

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号