簡體   English   中英

按列名逐行讀取 csv 文件

[英]Reading csv file row by column name

我有這個 csv 文件

id  title   link    description condition   price   availability    image_link  gtin    brand   mpn google_product_category product_type    item_group_id
438 Belkin 3m USB 2.0 Printer Extension Cable - Black   https://amazon.com/electronics/computers-components-and-accessories/printers-and-accessories/printers/belkin-3m-usb-2.0-printer-extension-cable-black/  Add length to your USB cables when they can't quite reach their destination with the USB A/B Extension Cable. Stop struggling with short cables while connecting your printer, scanner, or hard drive, thanks to this convenient extension cable. This USB 2.0 cable delivers error-free data transmissions at up to 480Mbps, letting you send jobs to external devices and transfer files to and from your computer quickly.Supports up to 127 devices on a daisy-chain configuration.3m cable.For connecting USB Cable.Manufacturer's limited lifetime guarantee.EAN: 722868246405.       11.00 GBP       https://amazon.com/images/thumbnails/590/590/detailed/9/3082276_R_Z001A.jpg                 Electronics > Computers, Components & Accessories > Printers & Accessories > Printers   
6   Toshiba 32C120U 32" Class 720P HD LCD TV    https://amazon.com/electronics/home-cinema-tv-and-video/plasma-tvs/toshiba-32c120u-32-class-720p-hd-lcd-tv/ Think you need to break your budget to enjoy blockbuster HD entertainment? Not with Toshiba's 32" class C110U LCD HDTV. Featuring all-new design, this value-packed television looks great and works well in a living room, kitchen or bedroom. Increase detail and depth of the images on your screen with our impressive Dynalight technology.        329.99 GBP      https://amazon.com/images/thumbnails/590/590/detailed/0/lcd-tv-32C120-01.jpg        Toshiba         Electronics > Home Cinema, TV & Video > Plasma TVs  
410 USB 2.0 A-Male to B-Male 1.8m Computer Cable    https://amazon.com/computers-and-accessories/usb-2.0-a-male-to-b-male-1.8m-computer-cable/  With the 1.8 meter cable, distance won't be an issue as you can link your scanner, printer or external hard drive to your computer even if they're far apart. As long as they are USB compatible, you can connect your devices with ease.The sturdy PVC housing and jacket means that your cable will be protected against wear and tear.1.8m cable.For connecting USB A/M TO USB B/M.Manufacturer's 1 year guarantee.      9.00 GBP        https://amazon.com/images/thumbnails/590/590/detailed/9/9214392_R_Z001A.jpg                 Computers & Accessories 

這基本上是一個 csv 具有以下列,

id  title   link    description condition   price   availability    image_link  gtin    brand   mpn google_product_category product_type    item_group_id

我正在嘗試閱讀專欄

<?php
$row = 1;
if (($handle = fopen("google_base.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo '<pre>';
            print_r($data);
            echo '</pre>';
        }
    }
    fclose($handle);
}

或者

<?php
$row = 1;
if (($handle = fopen("google_base.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
    echo '<pre>';
       print_r($data);
       echo '</pre>';
    }
    fclose($handle);
}

但是生成的 arrays 並沒有生成組織良好的行數據及其各自的列。

我可以查看此工具上的列https://www.convertcsv.com/csv-viewer-editor.htm

在此處輸入圖像描述

我怎樣才能按列名讀取一行?

使用制表符分隔符來讀取這樣的各個行

<?php
$row = 1;
if (($handle = fopen("google_base.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) { 
    echo '<pre>';
       echo $data[0].'</br>';
       echo '</pre>';
    }
    fclose($handle);
}

暫無
暫無

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

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