...

Convert php array to python list

sir pannels
04-30-2012, 03:25 PM
Hello,

I'm using Python 2.7 to send a request to a third party API... it returns some data which php can print using print_r..in python if I just try to print the data returned I get ...

{'packages': None, 'my_id': '1', 'tracks': [], 'this_id': '5', 'items': '12', 'row_total': None}

I'm not sure how to process this in Python.. I tried doing..

for item in data:
print item

..however that just outputs:
packages
my_id
tracks
this_id
items
row_total
None

..where really I need to be able to access the value of each of those keys...

Any pointers or assistance would be greatly appreciated.

Many thanks,
P :D

sir pannels
04-30-2012, 03:34 PM
Update:

As a crude way of dealing with this data I tried to just split the data by , like so...
split_data = data.split(',')
but it gave an error...

'dict' object has no attribute 'split'

/stuck :)

sir pannels
04-30-2012, 04:10 PM
I've worked this out now, so for anyone else having a similar problem understanding the php array -> python list issue.

In actual fact the data I posted on my first post in this thread is a dictionary, as far as Python is concerned, not a list.. to itterate through this dictionary in Python I did..


for key, value in shipment.iteritems():
print "Key: %s; Value: %s<br />" % (key, value)

Cheers,



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum