Hướng dẫn what is the better way to read the large csv file in php? - cách tốt hơn để đọc tệp csv lớn trong php là gì?

Sau khi đấu tranh rất nhiều, cuối cùng tôi cũng tìm thấy một giải pháp tốt, cũng có thể giúp đỡ người khác. Khi tôi đã thử tệp CSV 2.367kB chứa 18226 hàng, thời gian ít nhất được thực hiện bởi các tập lệnh PHP khác nhau là (1) từ tài liệu php.net fgetcsv có tên CsvImporter và (2)

.

Giải pháp tốt nhất tôi tìm thấy sử dụng tổng số 3.0644409656525 bao gồm thêm vào cơ sở dữ liệu và một số kiểm tra có điều kiện. Phải mất 11 giây trong việc xử lý một tệp 8MB. Giải pháp là:

$csvInfo = analyse_file($file, 5);
    $lineSeperator = $csvInfo['line_ending']['value'];
    $fieldSeperator = $csvInfo['delimiter']['value'];
    $columns = getColumns($file);
    echo '
========Details========
'; echo 'Line Sep: \t '.$lineSeperator; echo '
Field Sep:\t '.$fieldSeperator; echo '
Columns: ';print_r($columns); echo '
========Details========
'; $ext = pathinfo($file, PATHINFO_EXTENSION); $table = str_replace(' ', '_', basename($file, "." . $ext)); $rslt = table_insert($table, $columns); if($rslt){ $query = "LOAD DATA LOCAL INFILE '".$file."' INTO TABLE $table FIELDS TERMINATED BY '$fieldSeperator' "; var_dump(addToDb($query, false)); } function addToDb($query, $getRec = true){ //echo '
Query : '.$query; $con = @mysql_connect('localhost', 'root', ''); @mysql_select_db('rtest', $con); $result = mysql_query($query, $con); if($result){ if($getRec){ $data = array(); while ($row = mysql_fetch_assoc($result)) { $data[] = $row; } return $data; }else return true; }else{ var_dump(mysql_error()); return false; } } function table_insert($table_name, $table_columns) { $queryString = "CREATE TABLE " . $table_name . " ("; $columns = ''; $values = ''; foreach ($table_columns as $column) { $values .= (strtolower(str_replace(' ', '_', $column))) . " VARCHAR(2048), "; } $values = substr($values, 0, strlen($values) - 2); $queryString .= $values . ") "; //// echo $queryString; return addToDb($queryString, false); } function getColumns($file){ $cols = array(); if (($handle = fopen($file, 'r')) !== FALSE) { while (($row = fgetcsv($handle)) !== FALSE) { $cols = $row; if(count($cols)>0){ break; } } return $cols; }else return false; } function analyse_file($file, $capture_limit_in_kb = 10) { // capture starting memory usage $output['peak_mem']['start'] = memory_get_peak_usage(true); // log the limit how much of the file was sampled (in Kb) $output['read_kb'] = $capture_limit_in_kb; // read in file $fh = fopen($file, 'r'); $contents = fread($fh, ($capture_limit_in_kb * 1024)); // in KB fclose($fh); // specify allowed field delimiters $delimiters = array( 'comma' => ',', 'semicolon' => ';', 'tab' => "\t", 'pipe' => '|', 'colon' => ':' ); // specify allowed line endings $line_endings = array( 'rn' => "\r\n", 'n' => "\n", 'r' => "\r", 'nr' => "\n\r" ); // loop and count each line ending instance foreach ($line_endings as $key => $value) { $line_result[$key] = substr_count($contents, $value); } // sort by largest array value asort($line_result); // log to output array $output['line_ending']['results'] = $line_result; $output['line_ending']['count'] = end($line_result); $output['line_ending']['key'] = key($line_result); $output['line_ending']['value'] = $line_endings[$output['line_ending']['key']]; $lines = explode($output['line_ending']['value'], $contents); // remove last line of array, as this maybe incomplete? array_pop($lines); // create a string from the legal lines $complete_lines = implode(' ', $lines); // log statistics to output array $output['lines']['count'] = count($lines); $output['lines']['length'] = strlen($complete_lines); // loop and count each delimiter instance foreach ($delimiters as $delimiter_key => $delimiter) { $delimiter_result[$delimiter_key] = substr_count($complete_lines, $delimiter); } // sort by largest array value asort($delimiter_result); // log statistics to output array with largest counts as the value $output['delimiter']['results'] = $delimiter_result; $output['delimiter']['count'] = end($delimiter_result); $output['delimiter']['key'] = key($delimiter_result); $output['delimiter']['value'] = $delimiters[$output['delimiter']['key']]; // capture ending memory usage $output['peak_mem']['end'] = memory_get_peak_usage(true); return $output; }

Cách tốt hơn để đọc tệp CSV lớn là gì?

Làm thế nào để mở các tệp văn bản và CSV thực sự lớn..
Phương pháp số 1: Sử dụng các biên tập viên miễn phí. Cách tốt nhất để xem các tệp văn bản cực lớn là sử dụng trình soạn thảo văn bản. ....
Phương pháp số 2: Chia thành nhiều phần. ....
Phương pháp số 3: Nhập vào cơ sở dữ liệu. ....
Phương pháp số 4: Phân tích với các thư viện Python. ....
Phương pháp số 5: Với các công cụ cao cấp ..

Làm cách nào để chia một tệp CSV lớn thành nhiều tệp trong PHP?

Split CSV là một biểu mẫu HTML đơn giản và tập lệnh PHP để lấy một tệp CSV lớn và tách nó thành các tệp nhỏ hơn. Mã dự kiến ​​hàng đầu tiên là tên cột và nó sẽ thêm nó vào tất cả các tệp được tạo. Việc phân chia hàng hiện tại xảy ra ở 5000 hàng bao gồm các tiêu đề. Để thay đổi số đếm, đặt giá trị $ max_rows. is a simple html form and php script to take a large csv file and separate it into smaller files. The code expects the first row to be the column names and it will add this to all files created. The current row split happens at 5000 rows including the titles. To change the count set the $max_rows value.

Cách tốt nhất để xem tệp CSV là gì?

Mở tệp CSV đơn giản hơn bạn nghĩ. Trong hầu hết mọi trình soạn thảo văn bản hoặc chương trình bảng tính, chỉ cần chọn Tệp> Mở và chọn tệp CSV. Đối với hầu hết mọi người, tốt nhất là sử dụng chương trình bảng tính. Các chương trình bảng tính hiển thị dữ liệu theo cách dễ đọc và làm việc hơn so với trình chỉnh sửa văn bản.choose File > Open and select the CSV file. For most people, it is best to use a spreadsheet program. Spreadsheet programs display the data in a way that is easier to read and work with than a text editor.

Làm thế nào có thể đọc tệp CSV trong PHP?

Mở tệp PHP và ghi mã sau trong đó được giải thích trong các bước sau ...
Mở bộ dữ liệu CSV bằng hàm fopen.$ open = fopen ("Tên tệp. ....
Đọc một dòng bằng hàm fgetcsv ().....
Sử dụng một vòng lặp để lặp trong mỗi hàng dữ liệu.....
Đóng tệp đó bằng phương thức php fclose () ..