I have a Python script that outputs a bunch of text to PHP using print
and shell_exec()
. I also have to send a dictionary between the two, to store some script information. Here is a simplified version of my code:
Python
import json
# Lots of code here
user_vars = {'money':player.money}
print(user_vars)
print(json.dumps(user_vars))
which outputs (in PHP):
{'money': 0} {"money": 0}
So the same thing, except JSON has double quotes.
PHP
This is the code I would use if I wanted to use JSON (print(json.dumps(user_vars))
, which outputs {"money": 0}
, note double quotes) to transfer data:
<?php
$result = shell_exec('python JSONtest.py');
$resultData = json_decode($result, true);
echo $resultData["money"];
?>
I have two questions:
- Is there some reason I should use
print(user_vars)
instead ofprint(json.dumps(user_vars))
, or vice versa? - Is there some other way to transfer
user_vars
/json.dumps(user_vars)
without it being seen in the final PHP page without having to dump them in an actual.json
file? Or am I going about this problem in the wrong way?
My code was based on the question here.