簡體   English   中英

自動跳過csv文件php中的標題行

[英]Auto skip heading rows in csv File php

我正在處理csv文件可能包含標題的問題,因此,我們如何跳過該標題或其他信息並跳至主數據(csv數據)

CSV數據可能像:

   **Heading 1**
This is some extra text before Data
date: xx-xx-xxxx
country data: A,B,C

     *Then here starts the Data(comma separated values)*  

Col1,col2,col3, 
dataCol1,datacol2,datacol3 ....

那么我們如何跳到主要數據並處理該數據

任何幫助將不勝感激..正如我嘗試但無法修復它,謝謝

您需要找到某種分隔符或模式,以用於指示數據從何處開始。 例如:

  • 您知道標題是否總是一定數量的行嗎?
  • 在您的示例中,它后面是否總會有一個空行?
  • 等等

知道之后,您可以測試文件是否包含該模式/分隔符,然后跳到csv部分。

謝謝,但不確定,標題部分可能存在或不存在... – Abzkn

這就是竅門-如果存在的話,您需要找出將會出現的條件。 然后測試這些條件。 例如,如果您知道標題始終為4行,而下一行為空白行,則可以執行以下操作:

<?php
$f = file_get_contents($filename); //get everything in the file being processed

$file_lines = explode("\n", $f); //break up each line into an array we can process

$start_line = 0; //assume the header is not present and we'll start processing from line 1

if($file_lines[4] == ''){
    //header is present, so start processing from line 5
    $start_line = 5;
}

for($l = $start_line;$l < count($file_lines;$l++){
    //process each line
}

?>

這絕不是一個完美的解決方案,因為您的問題中存在一些未知數-因此,我必須假設:與元數據/標題行相比,csv列數據的行將更多。 為了幫助進行這種啟發式分析,我們還將排除所有“空”行。

如果我們可以進行此假設,則可以執行以下操作:

<?php

// define filepath... optionally validate
// with `is_file()` and `is_writable()`
$file = __DIR__ . '/data.csv';

// create an SplFileObject
$csv = new SplFileObject($file);

// set some flags to read file transparently
// as a csv. drop `SKIP_EMPTY` will ignore all
// blank lines as specified above
$csv->setFlags(
    SplFileObject::DROP_NEW_LINE |
    SplFileObject::READ_AHEAD    |
    SplFileObject::SKIP_EMPTY    | 
    SplFileObject::READ_CSV
);

// init an empty array to store rows
$rows  = [];

// an `SplFileObject` allows iteration
// over its contents with `foreach`.
foreach ($csv as $row) {
    // stash each row into a sub-array
    // indexed by its length (number of columns)
    $rows[count($row)][] = $row;
}

// `max()` will return the biggest sub-array
// which will be the column data according 
// to our assumptions stated above
$csvData = max($rows);

var_dump($csvData);

如果$file的內容包含:

       **Heading 1**
This is some extra text before Data
date: xx-xx-xxxx
country data: A,B,C

     *Then here starts the Data(comma separated values)*

Col1,col2,col3
dataCol1,datacol2,datacol3
dataCol1,datacol2,datacol3
dataCol1,datacol2,datacol3

我們應該期待以下結果:

    Array
(
    [0] => Array
        (
            [0] =>     country data: A
            [1] => B
            [2] => C
        )

    [1] => Array
        (
            [0] =>     Col1
            [1] => col2
            [2] => col3
        )

    [2] => Array
        (
            [0] =>     dataCol1
            [1] => datacol2
            [2] => datacol3
        )

    [3] => Array
        (
            [0] =>     dataCol1
            [1] => datacol2
            [2] => datacol3
        )

    [4] => Array
        (
            [0] =>     dataCol1
            [1] => datacol2
            [2] => datacol3
        )

)

看起來不錯-除了...行country data: A,B,C已被解析為有效行,因為它還包含兩個逗號。 這就是嘗試啟發式編程的問題。 我不知道這在您的特定用例中是否會成為問題。 如果是這樣,可能需要對上述方法進行一些改進。

參考文獻:

希望這可以幫助 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM