将包含unix时间戳的CSV文件拆分为7个不同的csv文件

时间:2012-02-16 14:39:14

标签: php parsing csv

我有一个大的2列csv文件(“data.csv”),有一周的数据。 第一列有unix时间戳 第二列有字符串:

1329548400,cats
1329548400,dogs
1329550200,fish
1329550200,cats
1329552000,dogs
1329552000,fish
1329584400,cats
1329584400,dogs
1329550200,cats

通过php,我需要将data.csv转换为7个文件,Sunday.csv,Monday.csv ... Saturday.csv

如果我能抓住每一排,我知道我可以做一些像

这样的事情
$temp = "data.csv";
$fileName = array_unique($temp);
foreach ($fileName as $row) {
    $theDay = date("Y", $row[firstcolumn]);
    if ($theDay == "Sunday") {
        $fileToWriteTo = "Sunday.csv";
        $f = fopen($fileToWriteTo, "a");
        fwrite($fileToWriteTo, $row . "\n");
        fclose($fileToWriteTo);
        }
    if ($theDay == "Monday") {
        $fileToWriteTo = "Monday.csv";
        $f = fopen($fileToWriteTo, "a");
        fwrite($fileToWriteTo, $row . "\n");
        fclose($fileToWriteTo);
        }
    }

问题是

1)我不知道如何读取data.csv的每一行,假设上面的语法是错误的 2)我承认我有点像一个php外行:)

...我离我有多远?我甚至在球场吗?

3 个答案:

答案 0 :(得分:0)

如果您正在读取文件,则需要创建一个句柄来读取该文件,然后获取每一行然后解析。

$handle = fopen('data.csv','r');
while($line = fgets($handle)){
   //Do any parsing here.
   $array = explode(',',$line);
   $timestamp = $array[0];
   $string = $array[1];
   //Do whatever else you need to do with it.
}

如果要从一个文件创建更多文件,请尝试阅读此内容。

http://php.net/manual/en/function.fopen.php

答案 1 :(得分:0)

也许这会完成这项工作。我还没有测试过。

$temp = "data.csv";
$fileData = file($temp);
$errors = 0;
$days = Array(fopen('Sunday.csv','a+'),
              fopen('Monday.csv','a+'),
              fopen('Tuesday.csv','a+'),
              fopen('Wednesday.csv','a+'),
              fopen('Thursday.csv','a+'),
              fopen('Friday.csv','a+'),
              fopen('Saturday.csv','a+'));
foreach($days as $fd) $errors += ($fd === FALSE);
if( ! $errors)
{
    foreach ($fileData as $row) {
        $columns=explode(',',$row);
        $theDay = date("w", $columns[0]);
        if(empty($days[$theDay])) continue; // maybe not valid date
        fwrite($days[$theDay], $row."\n");
    }
}
else
{
    echo "I couldn't open $errors of the files\n";
}
foreach($days as $fd) if($fd !== FALSE) fclose($fd);

答案 2 :(得分:0)

首先,不要在每次迭代时open file在循环之前完成。

$fps = array(); // fps = File Pointers
$days = array( 'Sunday', 'Monday', 'Tuesday', ...);
foreach( $days as $key => $name){
    $fps[ $name] = fopen( 'a', $name . '.csv');
    if( !$fps[ $name]){
        die("Cannot open $name.csv");
    }

    // Although I wouldn't rely on date's `l` format, because
    // it can be localized and script may just stop working
    // and rather use:
    $fps[ $key] = fopen( 'a', $name . '.csv');
    if( $fps[ $key])...
    // Sunday = 7 = 0, Monday = 1...
}

现在,您将通过date( 'w')来填写字段,这将给出数字0 - 星期日,6 - 星期六:

$day = date( 'w', $timestamp);

关于制作独特的结果......您可能需要使用in_array()之类的函数,并将所有已处理的行存储在一个数组中。

关于读取和写入CSV文件,有两个功能:fgetcsv()和。 fputcsv()。整个循环:

$temp = "data.csv";
$rows = array(); // List of unique itemps
$fp = fopen( $temp, 'r') or die( "Cannot open `$temp` for reading");
// Put here loop which will open all 7 files as mentioned above
// Optionally skip first row (it may contain header)
fgets( $fp);

// Read each row
while( ($array = fgetcsv( $fp, 10000, ',', '"') !== false){
    // Check unique row
    $row = implode( ',', $array);
    if( in_array( $row, $rows)){
         continue; // Skip this iteration
    }
    $rows[] = $row;

    $day = date( 'w', $array[0]);
    fputcsv( $fps[$day], $array);
}

并且不要忘记close所有文件:

fclose( $fp);
foreach( $fps as $fp){
    fclose( $fp);
}