Note that there are some explanatory texts on larger screens.

plurals
  1. POUnserialize 'nested' arrays in Python
    primarykey
    data
    text
    <p>I've been trying to unserialize an array coming into my python script. I have found some extensions to Python (like Scott Hurring's version: <a href="http://hurring.com/scott/code/python/serialize/" rel="nofollow">http://hurring.com/scott/code/python/serialize/</a>) that unserialize simple data structures like:</p> <pre><code>a:2:{s:7:"version";s:3:"457";s:12:"version_beta";s:3:"461";}; </code></pre> <p>My data looks like this (couple of nested arrays):</p> <pre><code>5:a:{i:17;s:2:"36";i:26;a:1:{i:0;s:2:"44";}i:18;s:0:"";i:24;s:0:"";i:19;a:1:{i:0;s:2:"40";}}s:3:"qty";s:1:"1";}s:7:"options";a:3:{i:0;a:7:{s:5:"label";s:13:"Build options";s:5:"value";s:35:"Place headset, leave fork untouched";s:11:"print_value";s:35:"Place headset, leave fork untouched";s:9:"option_id";s:2:"17";s:11:"option_type";s:5:"radio";s:12:"option_value";s:2:"36";s:11:"custom_view";b:0;}i:1;a:7:{s:5:"label";s:22:"Front derailleur mount";s:5:"value";s:33:"Frame with front derailleur mount";s:11:"print_value";s:33:"Frame with front derailleur mount";s:9:"option_id";s:2:"26";s:11:"option_type";s:8:"checkbox";s:12:"option_value";s:2:"44";s:11:"custom_view";b:0;}i:2;a:7:{s:5:"label";s:18:"Pre-order discount";s:5:"value";s:22:"10% pre-order discount";s:11:"print_value";s:22:"10% pre-order discount";s:9:"option_id";s:2:"19";s:11:"option_type";s:8:"checkbox";s:12:"option_value";s:2:"40";s:11:"custom_view";b:0;}}s:14:"bundle_options";a:2:{i:24;a:3:{s:9:"option_id";s:2:"24";s:5:"label";s:6:"Tiller";s:5:"value";a:1:{i:0;a:3:{s:5:"title";s:11:"Tiller 90";s:3:"qty";i:1;s:5:"price";d:0;}}}i:22;a:3:{s:9:"option_id";s:2:"22";s:5:"label";s:22:"Seat size and material";s:5:"value";a:1:{i:0;a:3:{s:5:"title";s:12:"Seat (large)";s:3:"qty";i:1;s:5:"price";d:0;}}}}s:20:"product_calculations";i:1;s:13:"shipment_type";s:1:"0";} </code></pre> <p>In PHP I can simply unserialize this and get a couple of nested array's:</p> <pre><code> Array ( [info_buyRequest] =&gt; Array ( [uenc] =&gt; aHR0cDovL3d3dy5yYXB0b2Jpa2UubmwvYmlrZXMvbWlkLXJhY2VyL21kaS1yYWNlci1mcmFtZS1raXQuaHRtbD9fX19TSUQ9VSZvcHRpb25zPWNhcnQ, [product] =&gt; 171 [related_product] =&gt; [bundle_option] =&gt; Array ( [22] =&gt; 69 [24] =&gt; 74 ) [options] =&gt; Array ( [17] =&gt; 36 [26] =&gt; Array ( [0] =&gt; 44 ) [18] =&gt; [24] =&gt; [19] =&gt; Array ( [0] =&gt; 40 ) ) [qty] =&gt; 1 ) [options] =&gt; Array ( [0] =&gt; Array ( [label] =&gt; Build options [value] =&gt; Place headset, leave fork untouched [print_value] =&gt; Place headset, leave fork untouched [option_id] =&gt; 17 [option_type] =&gt; radio [option_value] =&gt; 36 [custom_view] =&gt; ) [1] =&gt; Array ( [label] =&gt; Front derailleur mount [value] =&gt; Frame with front derailleur mount [print_value] =&gt; Frame with front derailleur mount [option_id] =&gt; 26 [option_type] =&gt; checkbox [option_value] =&gt; 44 [custom_view] =&gt; ) [2] =&gt; Array ( [label] =&gt; Pre-order discount [value] =&gt; 10% pre-order discount [print_value] =&gt; 10% pre-order discount [option_id] =&gt; 19 [option_type] =&gt; checkbox [option_value] =&gt; 40 [custom_view] =&gt; ) ) [bundle_options] =&gt; Array ( [24] =&gt; Array ( [option_id] =&gt; 24 [label] =&gt; Tiller [value] =&gt; Array ( [0] =&gt; Array ( [title] =&gt; Tiller 90° [qty] =&gt; 1 [price] =&gt; 0 ) ) ) [22] =&gt; Array ( [option_id] =&gt; 22 [label] =&gt; Seat size and material [value] =&gt; Array ( [0] =&gt; Array ( [title] =&gt; Seat (large) [qty] =&gt; 1 [price] =&gt; 0 ) ) ) ) [product_calculations] =&gt; 1 [shipment_type] =&gt; 0 ) </code></pre> <p>However I can't figure out how to get this or a similar structure in Python?</p> <p>Thanks for the help in suggesting json and pickle. I had tried these and get strange errors. </p> <p>If I use this little script:</p> <pre><code>import pickle import json data = 'a:2:{s:7:"version";s:3:"457";s:12:"version_beta";s:3:"461";};' my_array = json.loads(data) print my_array </code></pre> <p>Then I get:</p> <pre><code>File "unser.py", line 8, in &lt;module&gt; my_array = json.loads(data) File "/usr/lib/python2.6/json/__init__.py", line 307, in loads return _default_decoder.decode(s) File "/usr/lib/python2.6/json/decoder.py", line 319, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.6/json/decoder.py", line 338, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded </code></pre> <p>If I swap <code>json.loads</code> for <code>pickle.loads</code> I get:</p> <pre><code>Traceback (most recent call last): File "unser.py", line 8, in &lt;module&gt; my_array = pickle.loads(data) File "/usr/lib/python2.6/pickle.py", line 1374, in loads return Unpickler(file).load() File "/usr/lib/python2.6/pickle.py", line 858, in load dispatch[key](self) File "/usr/lib/python2.6/pickle.py", line 1180, in load_append value = stack.pop() IndexError: pop from empty list </code></pre> <p>I'm obviously missing something silly here...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload