Passing a Python list to php

旧时模样 提交于 2019-12-18 12:38:58

问题


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 calls a Python script (that part works) that returns a list of strings. I'd like to process this list in php.

When I try:

print mylist

in myscript.py, and then :

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

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

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!


回答1:


For instance:

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'];



回答2:


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.

The simplest thing would be to have python output something like a comma separated list

Adam,Barry,Cain

and use

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

on the php side to turn your string data back into an array.

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.




回答3:


Apparently your question was misleading. Was redirected here thinking this a solution for converting python lists to php array.

Posting a naive solution for ones wanting to convert lists to php array.

// 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"
    )

)



回答4:


$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
)


来源:https://stackoverflow.com/questions/17235467/passing-a-python-list-to-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!