2012-02-16 58 views
0

我有一个大的2列csv文件(“data.csv”)与数周的数据。 列一个Unix的时间戳 列中有两个具有字符串:将具有unix时间戳的CSV文件拆分为7个不同的csv文件

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)我不知道如何读DA的每一行ta.csv,假设上面的语法是错误的 2)我承认我的一个PHP门外汉:)

...我有多远?我甚至在大球场?

+0

可能重复[如何提取的PHP csv文件数据(http://stackoverflow.com/问题/ 2805427/how-to-extract-data-from-csv-file-in-php) – Gordon 2012-02-16 14:54:24

+0

有一个特殊的函数[fgetcsv](http://php.net/manual/en/function.fgetcsv.php)这可能会派上用场ʘ͜ʘ – HerrSerker 2012-02-16 14:58:35

回答

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文件,有2个功能:fgetcsv() resp。 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); 
} 
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

+0

Waaaah!每次有人读取并爆炸一个CSV文件而不是使用fgetCSV()时,一个elephpant会死亡 – 2012-02-16 15:14:41

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); 
+0

是的,在每次迭代中打开和关闭文件,路要走! * * :) – Vyktor 2012-02-16 15:01:34

+0

你说得对。也许我必须将文件描述符放在$ days数组中并在最后关闭它们。 – core1024 2012-02-16 15:02:53

+0

不检查文件是否成功打开。))(使用@Vyktor,所以我会得到有关评论的通知) – Vyktor 2012-02-16 15:09:50