【问题标题】:how do I pass many php variables to python如何将许多 php 变量传递给 python
【发布时间】:2013-08-29 08:44:55
【问题描述】:

我正在使用下面的代码来启动一个 python 脚本并将一个 php 变量传递给它。

$tmp = exec("python path/to/pythonfile.py $myVariable $mySecondVariable", $output);

这很好用,我的问题是我需要将 100 多个变量传递给 python 脚本。我不希望这条 exec 行变得非常长且难以管理。我还探索了使用以下代码传递 php 数组而不是变量:

$checked = array(
"key1"     => "1"
"key2"     => "1"
"key3"     => "1"
);
$checkedJson = json_encode($checked);
$tmp = exec("python path/to/pythonfile.py $myVariable $checkedJson", $output);

有了这个,我无法在 python 端解码 JSON。我已经能够在 python 中对数组变量(未解码)进行基本打印,但它会将每个单独的字符作为新的数组值。即[0] = k,[1] = e,[2] = y,[3] = 1,等等... 非常感谢任何帮助。

为了清楚起见,我正在寻找一种比编码和解码数组更简单的方法。有没有办法可以格式化 exec 行以允许多个变量。

【问题讨论】:

    标签: php arrays variables python-2.7 exec


    【解决方案1】:

    将您的 PHP 变量存储在一个临时文本文件中,然后使用 python 读取该文件。

    简单有效。


    假设脚本在同一目录中

    PHP 部分

    长版本(自包含脚本 - 如果您只想要代码 sn-p,请跳到下面的短版本)

    <?php
    
    #Establish an array with all parameters you'd like to pass. 
    #Either fill it manually or with a loop, ie:
    
    #Loop below creates 100 dummy variables with this pattern.  
    #You'd need to come up with a way yourself to fill a single array to pass
    #$variable1 = '1';
    #$variable2 = '2';
    #$variable3 = '3';
    #....
    #$variableN = 'N';
    #...    
    for ($i=1; $i<=100; $i++) {
        ${'variable'.$i} = $i;
    }
    
    #Create/Open a file and prepare it for writing
    $tempFile = "temp.dat";
    $fh = fopen($tempFile, 'w') or die("can't open file");
    
    #let's say N=100
    for ($i=1; $i<=100; $i++) {
    
        #for custom keys 
        $keyname = 'Key'.$i;
    
        # using a variable variable here to grab $variable1 ... $variable2 ... $variableN     ... $variable100
        $phpVariablesToPass[$keyname] = ${'variable'.$i} + 1000;
    
    }
    
    #phpVariablesToPass looks like this:
    # [Key1] => 1001 [Key2] => 1002 [Key3] => 1003  [KeyN] = > (1000+N)
    
    
    #now write to the file for each value.  
    #You could modify the fwrite string to whatever you'd like
    foreach ($phpVariablesToPass as $key=>$value) {
        fwrite($fh, $value."\n");
    }
    
    
    #close the file
    fclose($fh);
    
    ?>
    


    或者简而言之,假设 $phpVariablesToPass 是一个填充了您的值的数组:

    #Create/Open a file and prepare it for writing
    $tempFile = "temp.dat";
    $fh = fopen($tempFile, 'w') or die("can't open file");
    foreach ($phpVariablesToPass as $key=>$value) {
        fwrite($fh, $value."\n");
    }
    fclose($fh);
    


    获取数据的 Python 片段

    lines = [line.strip() for line in open('temp.dat')]
    

    变量 lines 现在将所有 php 数据包含为一个 python 列表。

    【讨论】:

    • 感谢您的建议,我会研究一下。
    • 如果您想要一个有关如何执行此操作的示例,请告诉我。我已经使用临时文件在语言/脚本之间移动了无数大型数据集,这确实是要走的路。
    • 当然会有帮助。
    • 希望这些示例有所帮助。确保设置了 temp.dat 权限,以便 php 可以打开/写入文件,python 可以打开/读取文件。主要关注sn-p的php短版。长版是一个独立的脚本,您可以将其用作模板。
    • 抱歉耽搁了,目前这是一个小项目。
    猜你喜欢
    • 1970-01-01
    • 2012-08-15
    • 2012-07-24
    • 1970-01-01
    • 2019-07-07
    • 2018-08-07
    • 2019-12-07
    • 1970-01-01
    • 2017-04-29
    相关资源
    最近更新 更多