在EOT中插入带有PHP变量的python代码

在EOT中插入带有PHP变量的python代码,php,python,Php,Python,我正在尝试用PHP生成一个Python脚本。PHP脚本在python代码中嵌入变量,然后输出生成的python脚本 使用时,主要的破坏是在结束EOT之前有额外的间距,但也充满了未定义的变量 所以这里是固定的: <?php // PHP Var $generateID = '123'; $data_sensor['device_id'] = 'xyz'; $data['heatControlRelay'] = '12345'; $date = '12/04/2017'; $id = '2

我正在尝试用PHP生成一个Python脚本。PHP脚本在python代码中嵌入变量,然后输出生成的python脚本


使用
时,主要的破坏是在结束
EOT
之前有额外的间距,但也充满了未定义的变量

所以这里是固定的:

<?php
// PHP Var
$generateID = '123';

$data_sensor['device_id'] = 'xyz';
$data['heatControlRelay'] = '12345';

$date = '12/04/2017';
$id = '2';
$phparray['value1'] = '03:45';
$phparray['value2'] = '08:00';
$phparray['value3'] = '17:00';
$phparray['value4'] = '21:30';

// Embed Variables in the Python script code to be later generated  
$PythonContents =  <<<EOT
    I'm, trying to embed "\$phpvar" inside python code.

    '''
    Python script generated on {$date}

    '''

    # Python Function specific imports
    import datetime

    ################    Python configs ############
    myvar = 'fixed'  
    function_id   = {$id}  
    Python-With-PHP-var-Range = [['{$phparray['value1']}','{$phparray['value2']}'],['{$phparray['value3']}','{$phparray['value4']}']]

    Result-IN-Python-range = [['03:45','08:00'],['17:00','21:30']]

    Python-and-PHP-var2 = {
        '{$generateID}/f/{$data_sensor['device_id']}/test/{$data['heatControlRelay']}/rstate': 'heatingisLocked',
    }

    ResultINPython = {
        '44/f/folder1/test/85111': 'Value',
    }
EOT;
echo $PythonContents;

What's not working?,你有未定义的变量。我本来以为会有更多的错误和复杂性,这太简单了:DNa很好,虽然我讨厌heredocs,但很难看,我个人会将python作为一个文件加载,替换其中的占位符,或者将其视为一个视图,这样更易于维护。祝你的项目好运。万分感谢,实际上你现在给了我一个新想法:)
<?php
// PHP Var
$generateID = '123';

$data_sensor['device_id'] = 'xyz';
$data['heatControlRelay'] = '12345';

$date = '12/04/2017';
$id = '2';
$phparray['value1'] = '03:45';
$phparray['value2'] = '08:00';
$phparray['value3'] = '17:00';
$phparray['value4'] = '21:30';

// Embed Variables in the Python script code to be later generated  
$PythonContents =  <<<EOT
    I'm, trying to embed "\$phpvar" inside python code.

    '''
    Python script generated on {$date}

    '''

    # Python Function specific imports
    import datetime

    ################    Python configs ############
    myvar = 'fixed'  
    function_id   = {$id}  
    Python-With-PHP-var-Range = [['{$phparray['value1']}','{$phparray['value2']}'],['{$phparray['value3']}','{$phparray['value4']}']]

    Result-IN-Python-range = [['03:45','08:00'],['17:00','21:30']]

    Python-and-PHP-var2 = {
        '{$generateID}/f/{$data_sensor['device_id']}/test/{$data['heatControlRelay']}/rstate': 'heatingisLocked',
    }

    ResultINPython = {
        '44/f/folder1/test/85111': 'Value',
    }
EOT;
echo $PythonContents;