如何在python中从php文件读取php数组
假设我们想从php制作一个python配置读取器 config.php如何在python中从php文件读取php数组,python,Python,假设我们想从php制作一个python配置读取器 config.php $arr = array( 'a config', 'b config', 'c config => 'with values' ) $arr2 = array( 'a config', 'b config', 'c config => 'with values' ) readconfig.py f = open('config.php', 'r') // so
$arr = array(
'a config',
'b config',
'c config => 'with values'
)
$arr2 = array(
'a config',
'b config',
'c config => 'with values'
)
readconfig.py
f = open('config.php', 'r')
// somehow get the array then turns it into pythons arr1 and arr2
print arr1
# arr1 = {'a config', 'b config', 'c config': 'with values'}
print arr2
# arr2 = {'a config', 'b config', 'c config': 'with values'}
这在python中可能吗
config.php
返回一个数组:
return [
'param1' => 'value1',
'param2' => 'value2',
];
作为真基因的变异: 在我的情况下,php文件只设置变量,不返回数组 因此,我必须在json_编码之前执行include;然后,对特定变量进行编码。这是为了阅读ownCloud的version.php。看 例如
使用ini或json或yaml或XML如果您为PHP数组语法编写解析器,那么这是可能的。我强烈建议你使用一种可移植的格式,比如YAML,甚至是一个带有普通
key=value
行的文件。我仍然没有得到2。三,。你能再解释一下吗?谢谢
return [
'param1' => 'value1',
'param2' => 'value2',
];
vFileName=configDict['ocDir']+'/version.php'
cmd=['/usr/bin/php','-r','include "'+vFileName+'"; echo json_encode(array($OC_Version,$OC_VersionString));']
ocVersion, ocVersionString=json.loads(subprocess.check_output(cmd))