V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
aihimmel
V2EX  ›  Python

为什么我的 python 不能 pickle 一个 dict?

  •  
  •   aihimmel · 2016-08-30 23:29:53 +08:00 · 4235 次点击
    这是一个创建于 3039 天前的主题,其中的信息可能已经有所发展或是发生改变。
    In [159]: a
    Out[159]:
    {'AveryJL': 4,
    'Railgunsssss': 8,
    u'SWER\u5fae': 4,
    u'TMD\u6b7b\u5ea6\u5a18': 8,
    'airman1': 4,
    'csh841486967': 4,
    'oi99o': 4,
    'uwookim': 4,
    'wy101574658': 8,
    'zzz1230456789': 4,
    u'\u4e03\u591c\u8bd7\u5e0c': 4,
    u'\u4e3f\u521d\u97f3Kong': 4,
    u'\u4e50\u56ed\u7684\u53a8\u5e08\u957f': 4,
    u'\u51b3\u65ad\u795e\u66f2': 4,
    u'\u58c1\u5427\u5427\u52a1\u7ec4': 4,
    u'\u6211\u662f\u6700\u840c\u7684\u5b69\u6b62': 4,
    u'\u6c64\u67d2\u67d2': 8,
    u'\u6dfa\u77b3Rory': 4,
    u'\u756a\u7ec4\u767e\u79d1': 8,
    u'\u83cabishi': 16,
    u'\u865a\u6570\u9b54\u672f': 4,
    u'\u94c3\u4e0e\u601c\u7269\u8bed': 4}

    In [160]: pickle.dumps(a)
    ---------------------------------------------------------------------------
    TypeError Traceback (most recent call last)
    <ipython-input-160-ffc7f744baa1> in <module>()
    ----> 1 pickle.dumps(a)

    c:\python27\lib\pickle.pyc in dumps(obj, protocol)
    1378 def dumps(obj, protocol=None):
    1379 file = StringIO()
    -> 1380 Pickler(file, protocol).dump(obj)
    1381 return file.getvalue()
    1382

    c:\python27\lib\pickle.pyc in dump(self, obj)
    222 if self.proto >= 2:
    223 self.write(PROTO + chr(self.proto))
    --> 224 self.save(obj)
    225 self.write(STOP)
    226

    c:\python27\lib\pickle.pyc in save(self, obj)
    284 f = self.dispatch.get(t)
    285 if f:
    --> 286 f(self, obj) # Call unbound method with explicit self
    287 return
    288

    c:\python27\lib\pickle.pyc in save_dict(self, obj)
    653
    654 self.memoize(obj)
    --> 655 self._batch_setitems(obj.iteritems())
    656
    657 dispatch[DictionaryType] = save_dict

    c:\python27\lib\pickle.pyc in _batch_setitems(self, items)
    666 if not self.bin:
    667 for k, v in items:
    --> 668 save(k)
    669 save(v)
    670 write(SETITEM)

    c:\python27\lib\pickle.pyc in save(self, obj)
    329
    330 # Save the reduce() output and finally memoize the object
    --> 331 self.save_reduce(obj=obj, *rv)
    332
    333 def persistent_id(self, obj):

    c:\python27\lib\pickle.pyc in save_reduce(self, func, args, state, listitems, di
    ctitems, obj)
    423
    424 if state is not None:
    --> 425 save(state)
    426 write(BUILD)
    427

    c:\python27\lib\pickle.pyc in save(self, obj)
    284 f = self.dispatch.get(t)
    285 if f:
    --> 286 f(self, obj) # Call unbound method with explicit self
    287 return
    288

    c:\python27\lib\pickle.pyc in save_dict(self, obj)
    653
    654 self.memoize(obj)
    --> 655 self._batch_setitems(obj.iteritems())
    656
    657 dispatch[DictionaryType] = save_dict

    c:\python27\lib\pickle.pyc in _batch_setitems(self, items)
    667 for k, v in items:
    668 save(k)
    --> 669 save(v)
    670 write(SETITEM)
    671 return

    c:\python27\lib\pickle.pyc in save(self, obj)
    304 reduce = getattr(obj, "__reduce_ex__", None)
    305 if reduce:
    --> 306 rv = reduce(self.proto)
    307 else:
    308 reduce = getattr(obj, "__reduce__", None)

    c:\python27\lib\copy_reg.pyc in _reduce_ex(self, proto)
    68 else:
    69 if base is self.__class__:
    ---> 70 raise TypeError, "can't pickle %s objects" % base.__name__
    71 state = base(self)
    72 args = (self.__class__, base, state)

    TypeError: can't pickle _Element objects
    10 条回复    2016-08-31 14:00:46 +08:00
    Yinz
        1
    Yinz  
       2016-08-30 23:54:33 +08:00
    StackOverflow 里找到说是 libxml 是 C 写的库, pickle 库可能不支持里面的部分对象
    http://stackoverflow.com/questions/8274438/saving-an-lxml-etree-elementtree-object
    aihimmel
        2
    aihimmel  
    OP
       2016-08-30 23:58:31 +08:00 via Android
    @Yinz 这里面有 lxml 的对象?
    Yinz
        3
    Yinz  
       2016-08-31 00:03:49 +08:00
    @aihimmel 既然报错回显里面说有 _Element 对象,那么具体出了什么问题就需要你自己排查了:D
    aihimmel
        4
    aihimmel  
    OP
       2016-08-31 00:22:43 +08:00 via Android
    @Yinz 好吧。。话说最上面我把这个 dict 输出了你看到了吗?
    RTNelo
        5
    RTNelo  
       2016-08-31 00:29:06 +08:00
    @aihimmel 我这测试是没有问题的。要不要使用 iPython 的 pdb 功能输出一下 proto 看看具体是哪个元素出问题了?
    aihimmel
        6
    aihimmel  
    OP
       2016-08-31 00:35:31 +08:00 via Android
    @RTNelo 谢谢,我明天试试。。
    wevsty
        7
    wevsty  
       2016-08-31 00:38:26 +08:00
    代码是不是使用了 multiprocessing ?
    aihimmel
        8
    aihimmel  
    OP
       2016-08-31 00:51:48 +08:00 via Android
    @wevsty 单线程的
    kkzxak47
        9
    kkzxak47  
       2016-08-31 12:13:12 +08:00 via Android
    一个字典,报的错却是_Element ,你不觉得奇怪?
    aihimmel
        10
    aihimmel  
    OP
       2016-08-31 14:00:46 +08:00 via Android
    @kkzxak47 就是觉得奇怪。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   994 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 21:00 · PVG 05:00 · LAX 13:00 · JFK 16:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.