将Python列表传递给php [英] Passing a Python list to php

查看:143
本文介绍了将Python列表传递给php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对php还是很陌生,我一直在花一些类型的知识来理解如何将参数从Python传递给php,反之亦然. 我现在知道如何传递单个变量,但是我仍然很困惑,似乎无法找到对此的答案:

I'm very new to php and I've been spending quite some type understanding how to pass arguments from Python to php and conversely. I now know how to pass single variables, but I am still stuck and can't seem to find an answer to this one:

Php调用Python脚本(该部分有效),该脚本返回字符串列表.我想在php中处理此列表.

Php calls a Python script (that part works) that returns a list of strings. I'd like to process this list in php.

当我尝试时:

print mylist

在myscript.py中,然后:

in myscript.py, and then :

$result = exec('python myscript.py')

似乎php将$ result理解为单个字符串(我同意这是很有意义的.)

it looks like php understands $result as a single string (which I agree makes sense).

我了解json可能会有所帮助,或者我需要以某种方式使用字典而不是python中的列表.但是我不知道具体如何.

I understand that maybe json can help or that I somehow need to use a dictionary instead of a list in python. However I can't figure out how exactly.

如果有人可以提供帮助,将不胜感激!谢谢!

If anyone can help, it will be much appreciated! Thanks!

推荐答案

例如:

myscript.py

import json

D = {'foo':1, 'baz': 2}

print json.dumps(D)

myscript.php

<?php 

$result = json_decode(exec('python myscript.py'), true);
echo $result['foo'];

这篇关于将Python列表传递给php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆