我怎么通过大量的PHP变量到Python [英] how do I pass many php variables to python

查看:165
本文介绍了我怎么通过大量的PHP变量到Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code发起一个python脚本和PHP变量传递给它。

I am using the following code to initiate a python script and pass a php variable to it.

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

这工作得非常好,我的问题是,我需要传递100+变量的python脚本。我不希望此exec线变得非常长,难以管理。我还探索通过一个PHP数组,而不是一个变量具有以下code:

This works very well, my issue is that I will need to pass 100+ variables to the python script. I don't want this exec line to become extremely long and unmanageable. I have also explored passing a php array instead of a variable with the following code:

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

有了这个,我已经无法去code中的JSON在Python端。我已经能够做到在python数组变量(理解过程codeD)的一个基本的打印,但它给每一个人的性格作为一个新的数组值。即[0] = K,[1] = E [2] = Y [3] = 1,等...
任何帮助是极大的AP preciated。

With this I have been unable to decode the JSON on the python side. I have been able to do a basic print of the array variable(undecoded) in python, but it gives every individual character as a new array value. ie [0] = k, [1] = e, [2] = y, [3] = 1, etc... Any help is greatly appreciated.

只是要清楚,我找了一个比简单的编码方法和解码的数组。有没有一种方法,我可以格式化EXEC线,以允许多个变量。

Just to be clear,I am looking for a simpler method than encoding and decoding an array. Is there a way I can format the exec line to allow for multiple variables.

推荐答案

一个临时文本文件中存储你的PHP变量,然后使用Python来读取该文件。

Store your PHP variables within a temporary text file then use python to read that file.

简单有效。

假设脚本是在同一目录

PHP部分

长版(自包含的脚本 - 跳到下面的短版,如果你只想要code段)

<?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')]

变量的的现在包含了所有你的PHP数据作为一个Python列表。

the variable lines now contains all of your php data as a python list.

这篇关于我怎么通过大量的PHP变量到Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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