drwu24647 2012-12-14 13:26
浏览 224
已采纳

用PHP解析Python数组

I have a Python script running on my server that creates a 2D list like so:

[['Header1', 'Header2', 'Header3'], ['2012-09-10 00:11:00', '61.3', '57.0'], ...]

I pass this back to the PHP script which is running my webpage. Here's the PHP I'm currently using to get the array. I get the rows, but obviously with an unwanted [[ at the start and ]] at the end.

exec("python weatherdata.py $stationID $startdate $enddate", $pyoutput);
$vars = explode("], [", $pyoutput[0]);

For the sake of explaining what I actually want to do, since there's bound to be a "proper" solution (I'm not at all familiar with PHP), what I want to do is adapt the code from here which download a CSV file to the user, but uses mySQL to populate it. The set-up part here works fine.

Edited in response to Jack's answer below

// Commented out my working parsing part
// remove the start and end square braces then split into rows
//$output = str_replace("[[","",$pyoutput);
//$output = str_replace("]]","",$output);
//$rows = explode("], [", $output[0]);

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='data.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

foreach (preg_split('/],\s*\[/', trim($pyoutput, '[]')) as $row) {
        $data= preg_split("/',\s*'/", trim($row, "'"));
        fputcsv($data);
}
// Commented out my working write to CSV part
// write rows
//foreach ($rows as $row){
//    $row = str_replace("'","",$row);
//    $row = explode(",", $row);
//    fputcsv($output, $row);
//}
  • 写回答

1条回答 默认 最新

  • doujiu5464 2012-12-14 13:39
    关注

    Not sure whether this is viable for you, but since PHP 5.4 it supports the short array syntax, e.g. [1, 2, 3] instead of array(1, 2, 3).

    So, you could just use evil ... I mean eval():

    $vars = eval(`python weatherdata.py $stationID $startdate $enddate`);
    

    Otherwise, if the array syntax is always in that format, just break it apart with preg_split(), first on square brackets and then on single quotes:

    foreach (preg_split('/],\s*\[/', trim($s, '[]')) as $row) {
            $data = preg_split("/',\s*'/", trim($row, "'"));
            print_r($data);
    }
    

    Output:

    Array
    (
        [0] => Header1
        [1] => Header2
        [2] => Header3
    )
    Array
    (
        [0] => 2012-09-10 00:11:00
        [1] => 61.3
        [2] => 57.0
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP