简体   繁体   English

将Python列表传递给php

[英]Passing a Python list to 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. 我对php非常陌生,而且我已经花了很多时间来理解如何将参数从Python传递到php,反之亦然。 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 calls a Python script (that part works) that returns a list of strings. Php调用Python脚本(该部分有效),返回字符串列表。 I'd like to process this list in php. 我想在php中处理这个列表。

When I try: 当我尝试:

print mylist

in myscript.py, and then : 在myscript.py中,然后:

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

it looks like php understands $result as a single string (which I agree makes sense). 看起来像php将$ result理解为单个字符串(我同意这是有道理的)。

I understand that maybe json can help or that I somehow need to use a dictionary instead of a list in python. 我知道也许json可以帮助或者我在某种程度上需要使用字典而不是python中的列表。 However I can't figure out how exactly. 但是我无法弄清楚到底是怎么回事。

If anyone can help, it will be much appreciated! 如果有人可以提供帮助,我们将不胜感激! Thanks! 谢谢!

For instance: 例如:

myscript.py myscript.py

import json

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

print json.dumps(D)

myscript.php myscript.php

<?php 

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

You're using stdin / stdout to transfer the data between the programs, that means you'll have to encode your structure somehow in order to let your receiving program parse the elements. 您正在使用stdin / stdout在程序之间传输数据,这意味着您必须以某种方式对结构进行编码,以便让接收程序解析元素。

The simplest thing would be to have python output something like a comma separated list 最简单的方法是让python输出类似于逗号分隔列表

Adam,Barry,Cain 亚当,巴里,该隐

and use 并使用

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

on the php side to turn your string data back into an array. 在PHP端将您的字符串数据转换回数组。

If the data is unpredictable (might contain commas) or more structured (more than just a simple list) then you should go for something like json as suggested by Krab. 如果数据是不可预测的(可能包含逗号)或更多结构化(不仅仅是一个简单的列表),那么你应该按照Krab的建议去寻找像json这样的东西。

Apparently your question was misleading. 显然你的问题是误导性的。 Was redirected here thinking this a solution for converting python lists to php array. 被重定向到这里认为这是一个将python列表转换为php数组的解决方案。

Posting a naive solution for ones wanting to convert lists to php array. 为想要将列表转换为php数组的人发布一个天真的解决方案。

// Sample python list 
$data = '[["1","2","3","4"],["11","12","13","14"],["21","22","23","24"]]';

// Removing the outer list brackets
$data =  substr($data,1,-1);

$myArr = array();
// Will get a 3 dimensional array, one dimension for each list
$myArr =explode('],', $data);

// Removing last list bracket for the last dimension
if(count($myArr)>1)
$myArr[count($myArr)-1] = substr($myArr[count($myArr)-1],0,-1);

// Removing first last bracket for each dimenion and breaking it down further
foreach ($myArr as $key => $value) {
 $value = substr($value,1);
 $myArr[$key] = array();
 $myArr[$key] = explode(',',$value);
}

//Output
Array
(
[0] => Array
    (
        [0] => "1"
        [1] => "2"
        [2] => "3"
        [3] => "4"
    )

[1] => Array
    (
        [0] => "11"
        [1] => "12"
        [2] => "13"
        [3] => "14"
    )

[2] => Array
    (
        [0] => "21"
        [1] => "22"
        [2] => "23"
        [3] => "24"
    )

)
$str = "[u'element1', u'element2', 'element3']";

$str = str_replace( array("u'", "[", "]"), array("'", ""), $str );
$strToPHPArray = str_getcsv($str, ",", "'");
print_r( $strToPHPArray );

Outputs 输出

Array
(
    [0] => element1
    [1] => element2
    [2] => element3
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM