Reputation: 319
How do I parse a dict output from a python script to a PHP array? My PHP file contains the line exec('/bin/python ~/somescript.py', $out, $err);
. And my python script prints a dict with something like
d = {0: "a", 7: "d", 23: ["e", "z"]}
print(d)
But this doesn't work, $out[0]
in my PHP file is just a really long string an array. I've also tried to use print(json.dumps(d))
, same result (but with escaped " ).
Upvotes: 1
Views: 357
Reputation: 15962
Use print(json.dumps(d))
in your Python script. Trim the dumps
output by passing it separators=(',', ':')
with the default indent=None
so that the JSON is dumped to one line. So:
print(json.dumps(d, indent=None, separators=(',', ':')))
After that json_decode($out)
or json_decode($out[0])
in your PHP script.
Upvotes: 1