Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-30-2012, 03:25 PM   PM User | #1
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
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
sir pannels is offline   Reply With Quote
Old 04-30-2012, 03:34 PM   PM User | #2
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
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 is offline   Reply With Quote
Old 04-30-2012, 04:10 PM   PM User | #3
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
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,
sir pannels is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:13 PM.


Advertisement
Log in to turn off these ads.