zorian15
zorian15

Reputation: 55

How to return a dictionary created in PHP into Python?

Due to some restrictions on what I'm allowed to use, I had to create a dictionary with arrays as values in PHP. At the end of my script, I put a return statement to return the dictionary. I now have to run a clustering algo in Python with this map. I tried to run

proc = subprocess.call(["php", "/path/to/file/file.php"])

To store the dictionary as proc. I then printed the type of proc and then the value just to have an idea if it stored the data correctly, however, the type was int, and the value was 0. (The keys in this dictionary are all strings, with arrays of integers as the values). Any idea on why I'm getting these return values? Or a better way to import my dictionary to Python 2.7?

Thanks!

Upvotes: 0

Views: 275

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113988

I struggle to comprehend a senario where this is a good idea...but regardless

test.php

<?php
$data = array("1"=>2,"3"=>5,"asd"=>22,"bob"=>"susan");
echo json_encode($data);
?>

python

>>> import subprocess,json
>>> json.loads(subprocess.check_output("php test.php",shell=True))
{u'1': 2, u'3': 5, u'bob': u'susan', u'asd': 22}

Upvotes: 2

Related Questions