CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Python (http://www.codingforums.com/forumdisplay.php?f=46)
-   -   Convert php array to python list (http://www.codingforums.com/showthread.php?t=259306)

sir pannels 04-30-2012 03:25 PM

Convert php array to python list
 
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 ...

Quote:

{'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..
Code:

for item in data:
      print item

..however that just outputs:
Quote:

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...
Code:

split_data = data.split(',')
but it gave an error...

Quote:

'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..

Code:

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

Cheers,


All times are GMT +1. The time now is 07:17 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.