Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中从php文件读取php数组_Python - Fatal编程技术网

如何在python中从php文件读取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

假设我们想从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')
// 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中可能吗

  • 使用PHP脚本解析配置

  • 使用JSON将配置变量保存到文件

  • 使用os.system()从Pytson执行解析器

  • 在Python中读取JSON文件

  • 其中
    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))