开发者

collect values from csv file?

I have a csv file with traceroutes, the last entry in every row is the target IP, but as there are variable no of records in every row ...s开发者_Python百科o I am having difficulty I want to collect the last entry from every row into a column

how do I do that?


PHP has a nice function fgetcsv().

Example:

if (($handle = fopen("x.csv", "r")) !== FALSE)//your file
{
    echo "<table><tr><th>Row</th><th>ID</th><th>Name</th></tr>";
    $num_rows = 7440; //number of rows in your CSV file.
    for($w=0; $w<$num_rows; $w++)
    {
        $data = fgetcsv($handle, 1000, ",");
        echo "<tr>
        <td>{$w}</td>
        <td>{$data[0]}</td>
        <td>{$data[1]}</td>
        </tr>";
    }
    echo "</table>";
    fclose($handle);
}
else
{
    die('fopen failed');
}

To collect the value from the last cell in each row, do the following inside the for loop:

echo $data[(count($data)-1)];

Although, this only needs to be done if the number of cells in each row varies or if you actually don't know.
Otherwise do what's in the example above and just specify it; e.g. if the last cell in each row is always in column C then you collect the value from $data[2] (in the for loop)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜