php 使用php逐行解析csv文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8320372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:25:39  来源:igfitidea点击:

parse csv file using php line by line

phpcsv

提问by JDesigns

I have a csv file with data row 1 is header like this

我有一个 csv 文件,数据行 1 是这样的标题

 id fname lname email date code pid

each row then has data below each field name

每行然后在每个字段名称下方都有数据

3232456454  mike    strong  [email protected]   11/8/11 0:00    AU  2540

and when i see this csv using text editor, i see it as below.

当我使用文本编辑器看到这个 csv 时,我看到它如下。

3232456454,mike,strong,[email protected],11/8/11 0:00,AU,2540
87876548788,bob,cool,[email protected],11/8/11 0:00,RY,2148
23498765,nike,tick,[email protected],11/8/11 0:00,TE,5240

Now i want to read the file using php and i want the data to be exactly same format as text file. I will use this data for other purpose as it is. I don't want to split at commas so i cannot use fgetcsv. I tried by just opening it using php file() but when i loop it, i don't see the proper data. Can some one please throw some suggestions?

现在我想使用 php 读取文件,并且我希望数据与文本文件的格式完全相同。我会将这些数据原样用于其他目的。我不想用逗号分割,所以我不能使用 fgetcsv。我尝试使用 php file() 打开它,但是当我循环它时,我没有看到正确的数据。有人可以提出一些建议吗?

if I do this way, it prints in the array with each element taking one value and I don't want this..

如果我这样做,它会打印在数组中,每个元素取一个值,我不想要这个..

    $csv = array();
    $lines = file('sample.csv', FILE_IGNORE_NEW_LINES);

    foreach ($lines as $key => $value)
    {
        $csv[$key] = str_getcsv($value);
    } 
print_r($csv)

I tried using parsecsv library from google code but this is not really necessary as i want the data line by line.

我尝试使用谷歌代码中的 parsecsv 库,但这并不是真正必要的,因为我想要逐行数据。

I need to ignore the first line(header) in the output.

我需要忽略输出中的第一行(标题)。

If i can get the output like this, this will solve my issues

如果我能得到这样的输出,这将解决我的问题

Array
(
    [id,fname,lname,email,,data,code,pid] => 3232456454,mike,strong,[email protected],11/8/11 0:00,AU,2540
)

Array
(
    [id,fname,lname,email,,data,code,pid] => 87876548788,bob,cool,[email protected],11/8/11 0:00,RY,2148
)

regards

问候

回答by deceze

$csv = file_get_contents($file);

This will get the file as iswithout any conversions or splitting or anything, just one long string.

这将按原样获取文件无需任何转换或拆分或任何操作,只有一个长字符串。

If you want to split it into an array for each line, simply do

如果要将其拆分为每行的数组,只需执行

$csv = file($file, FILE_IGNORE_NEW_LINES);

and nothing else. No looping, no getcsv, just file().

没有别的。没有循环,没有getcsv,只是file()



But even if you say you're not interested in parsing the CSV, you aredealing with a CSV so I'd parse it as soon as possible and output it again, even back to CSV if necessary.

但即使你说你对解析 CSV 不感兴趣,你正在处理一个 CSV,所以我会尽快解析它并再次输出它,甚至在必要时返回到 CSV。

$fh = fopen($file, 'r');
$header = fgetcsv($fh);

$data = array();
while ($line = fgetcsv($fh)) {
    $data[] = array_combine($header, $line);
}

fclose($fh);

print_r($data);


// outputting:

$out = fopen('php://output', 'w');
fputcsv($out, array_keys($data[0])); // skip this if you don't want headers
foreach ($data as $line) {
    fputcsv($out, $line);
}
fclose($out);

If line endings aren't properly recognized, try ini_set('auto_detect_line_endings', true);on top of your script.

如果无法正确识别行尾,请尝试ini_set('auto_detect_line_endings', true);在脚本之上。

See here for a demo: http://codepad.viper-7.com/t31IEv

在这里查看演示:http: //codepad.viper-7.com/t31IEv

回答by Robot Woods

Use fopen and fgets instead, and use a counter of which line you're at so you can ignore the first:

使用 fopen 和 fgets 代替,并使用您所在行的计数器,以便您可以忽略第一行:

$c=0;
$data=fopen($file,'r');
while($row=fgets($data)){
    //$row is your line as a string
    if($c!=0){
    //do something with it
    echo $row."\n";
   }
   $c++;
}

回答by Pierre-Yves

If no matter what you do you can only get the last line of your CSV file using fgets() or fgetcsv(), simply try to copy/paste the content of your file to a new one and try using it instead.

如果无论您做什么,都只能使用 fgets() 或 fgetcsv() 获取 CSV 文件的最后一行,只需尝试将文件内容复制/粘贴到新文件中,然后尝试使用它即可。

I'm referring to the issue that @Jay seems to had: It seems that if the file was created with Excel 2011/Mac, its structure is not correctly interpreted.

我指的是@Jay 似乎存在的问题:似乎如果文件是使用 Excel 2011/Mac 创建的,则其结构没有正确解释。

See this for more info: Which encoding opens CSV files correctly with Excel on both Mac and Windows?

有关详细信息,请参阅此内容:哪种编码可以在 Mac 和 Windows 上使用 Excel 正确打开 CSV 文件?