Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

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

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

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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...

    'dict' object has no attribute 'split'
    /stuck

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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,


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •