我正在使用库phpserialize开发一个python应用程序。问题是,现在我遇到了一个错误,我无法找到解决问题的方法: 我从php Web服务获取此序列化数据:
a:5:{s:10:"attributes";a:53:{s:9:"entity_id";s:2:"97";s:14:"entity_type_id";s:1:"4";s:16:"attribute_set_id";s:1:"4";s:7:"type_id";s:6:"simple";s:3:"sku";s:5:"16101";s:10:"created_at";s:19:"2013-05-29 14:59:05";s:10:"updated_at";s:19:"2013-08-08 07:38:41";s:11:"has_options";s:1:"1";s:16:"required_options";s:1:"1";s:4:"name";s:65:"Cuchillo Tactico Combat-Tec negro Aitor dureza de 55/58 HRc 16101";s:10:"meta_title";s:87:"Comprar Cuchillo Tactico Combat-Tec negro Aitor dureza de 55/58 HRc 16101 8435076573875";s:16:"meta_description";s:200:"Comprar Cuchillo Tactico Combat-Tec negro Aitor dureza de 55/58 HRc 16101 8435076573875 hasta un 50% más económico que en tiendas. Miles de referencias a tu disposición. EnvÃo Urgente. Infórmate!";s:5:"image";s:17:"/1/6/16101_37.png";s:11:"small_image";s:17:"/1/6/16101_37.png";s:9:"thumbnail";s:17:"/1/6/16101_37.png";s:7:"url_key";s:39:"cuchillo-tactico-combat-tec-negro-aitor";s:8:"url_path";s:44:"cuchillo-tactico-combat-tec-negro-aitor.html";s:13:"custom_design";N;s:17:"options_container";s:10:"container2";s:22:"gift_message_available";N;s:11:"page_layout";N;s:22:"country_of_manufacture";N;s:12:"msrp_enabled";s:1:"2";s:30:"msrp_display_actual_price_type";s:1:"4";s:7:"videoid";N;s:13:"c2c_long_hoja";s:4:"16.5";s:16:"c2c_espesor_hoja";s:3:"4.5";s:12:"manufacturer";N;s:5:"color";s:1:"1";s:6:"status";s:1:"1";s:12:"tax_class_id";s:1:"5";s:10:"visibility";s:1:"4";s:21:"enable_googlecheckout";s:1:"1";s:12:"is_recurring";s:1:"0";s:11:"is_imported";s:1:"0";s:9:"proveedor";s:2:"43";s:5:"price";s:7:"57.8100";s:6:"weight";s:6:"0.0000";s:13:"special_price";N;s:4:"cost";N;s:4:"msrp";N;s:17:"special_from_date";N;s:15:"special_to_date";N;s:14:"news_from_date";N;s:12:"news_to_date";N;s:18:"custom_design_from";N;s:16:"custom_design_to";N;s:11:"description";s:1672:"<p>Cuchillo AITOR modelo Combat-Tec con hoja negra de 16,5 Cm. con sierra superior. Acero Molibdeno Vanadio con prueba de dureza. Mango de micarta negra. Incluye funda de poliamida negra.</p><br /><table id="product-attribute-specs-table" class="data-table"><colgroup><col width="25%" /> <col /> </colgroup><br /><tbody><br /><trclass="first odd"><th class="label">Código</th><br /><td class="data last">16101</td><br /></tr><br /><tr class="even"><th class="label">Marcas</th><br /><td class="data last">Aitor</td><br /></tr><br /><tr class="odd"><thclass="label">Longitud hoja</th><br /><td class="data last">16.5</td><br /></tr><br /><tr class="even"><th class="label">Longitud total</th><br /><td class="data last">28</td><br /></tr><br /><tr class="odd"><th class="label">Material hoja</th><br /><td class="data last">Acero Aitor al cromo negro</td><br /></tr><br /><trclass="even"><th class="label">Funda</th><br /><td class="data last">No</td><br /></tr><br /><tr class="odd"><th class="label">Espesor hoja</th><br /><td class="data last">4.5</td><br /></tr><br /><tr class="even"><th class="label">Material mango</th><br /><td class="data last">Micarta</td><br /></tr><br /><tr class="odd"><th class="label">Presentación</th><br /><td class="data last">Estuche de cartón</td><br /></tr><br /><tr class="even"><thclass="label">Observaciones</th><br /><td class="data last">No</td><br /></tr><br /><tr class="odd"><th class="label">Peso</th><br /><td class="data last">291</td><br /></tr><br /><tr class="last even"><th class="label">Código EAN</th><br /><td class="data last">8435076573875</td><br /></tr><br /></tbody><br /></table>";s:17:"short_description";s:88:"Cuchillo Tactico Combat-Tec negro de la marca Aitor dureza de 55/58 HRc Referencia 16101";s:12:"meta_keyword";s:87:"Comprar Cuchillo Tactico Combat-Tec negro Aitor dureza de 55/58 HRc 16101 8435076573875";s:20:"custom_layout_update";N;s:10:"is_salable";s:1:"1";s:10:"stock_item";O:13:"Varien_Object":7:{s:8:"*_data";a:1:{s:11:"is_in_stock";s:1:"1";}s:18:"*_hasDataChanges";b:0;s:12:"*_origData";N;s:15:"*_idFieldName";N;s:13:"*_isDeleted";b:0;s:16:"*_oldFieldsMap";a:0:{}s:17:"*_syncFieldsMap";a:0:{}}}s:6:"custom";a:2:{s:9:"drop_down";a:1:{s:5:"abcde";a:3:{i:0;s:3:"123";i:1;s:3:"321";i:2;s:3:"231";}}s:5:"radio";a:1:{s:10:"Jajejijoju";a:3:{i:0;s:1:"G";i:1;s:1:"M";i:2;s:1:"P";}}}s:10:"categorias";a:5:{i:0;a:1:{s:16:"Default Category";s:12:"Root Catalog";}i:1;a:1:{s:7:"Default";s:16:"Default Category";}i:2;a:1:{s:12:"CuchillerÃa";s:7:"Default";}i:3;a:1:{s:9:"Cuchillos";s:12:"CuchillerÃa";}i:4;a:1:{s:31:"Cuchillos Militares y tácticos";s:9:"Cuchillos";}}s:10:"inventario";s:8:"999.0000";s:7:"galeria";a:1:{i:0;a:3:{s:3:"url";s:71:"http://foo.es/bar.png";s:5:"label";s:0:"";s:8:"position";s:1:"1";}}}
在反序列化时,会显示以下错误:
引发ValueError('序列化转储中的对象但是' ValueError:序列化转储中的对象,但未给出object_hook。
我已经用Google搜索了错误,但我最终只是在源代码中。 你有什么想法吗?
提前致谢。
答案 0 :(得分:1)
PyPI page of phpserialize上的文档包含一些文档,即:
对象钩子是一个获取类名和a的函数 以实例数据作为参数的dict。实例数据键是 在PHP格式,通常不是你想要的。把它转换成 Python标识符可以使用convert_member_dict函数。对于 有关它的更多信息,请看下一节。在这里 一个简单的对象钩子的例子:
>>> class User(object): ... def __init__(self, username): ... self.username = username ... >>> def object_hook(name, d): ... cls = {'WP_User': User}[name] ... # The following was added by me -Antti ... d = convert_member_dict(d) ... return cls(**d) ... >>> user = loads(data, object_hook=object_hook) >>> user.username 'admin'
因此,对于直接不支持phpserialize的所有类,您需要在Python中编写匹配的类,并从object_hook
返回它们的实例。