fgetcsv没有正确读取csv文件,保存在linux系统中 [英] fgetcsv not reading csv file properly that is saved in linux system

查看:139
本文介绍了fgetcsv没有正确读取csv文件,保存在linux系统中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过csv文件导入一些数据到我的数据库。问题是,在开始的开始我使用windows所以我的csv文件是写在ms办公室。所以当进口在那里工作正常。但切换到linux(UBUNTU),同时保存csv文件,它不显示我想要的数据。



这是一个由csv生成的数组,由ms office在Windows机器上生成,工作正常。 p>

 数组

[0] => Array

[0 ] =>学生姓名
[1] =>学生卷
[2] =>课程
[3] =& Exam
[5] =>主题
[6] =>总分
[7] =>成绩
[8] =>目标
[9] =>主观
[10] =>实用


[1] =& =>样品名称
[1] => 123
[2] => Nine
[3] => A
[4] =&
[5] => Math
[6] => 80
[7] => A +
[8] => 40
[9] ] => 20
[10] => 20



但在linux我的csv格式不能正常工作。它将所有的数据推入一个键&一个元素如下所示。

 数组

[0] =& (
[0] =>学生姓名学生卷类别科目考试科目总标记成绩目标主观实践


[1] => Array

[0] => Samp0le名称123九个中期数学80 A + 40 20 20





所以我的实际问题是我该如何读取?



上层数组生成:

  $ csv = array() 
// $ file = fopen('myCSVFile.csv','r');
ini_set(auto_detect_line_endings,true);
while(($ result = fgetcsv($ fp))!== false)
{
$ csv [] = $ result;
}

fclose($ fp);

echo'< pre>';
print_r($ csv);
echo'< / pre>';

请帮助我。非常需要。

解决方案

试试这个

  $ file =FNAC_SPECTACLES.csv ; 
$ handle = fopen($ file,r);
$ row = 1;
while(($ data = fgetcsv($ handle,0,;,'))!== FALSE)
{
if($ row == 1)
{
//跳过第一行
}
else
{
echo< pre>;
print_r($ data);
}
$ row ++;
}

fgetcsv()在第三个参数中在分隔符中要保存在csv文件中。例如:逗号,分号等。


I'm trying to import some data through csv file into my database. The problem is that in the very beginning of the development I was using windows so my csv file was written on ms office. So while importing there it works fine. But switching to linux(UBUNTU) while saving the csv file it's not showing data as i want. I'm not getting any solution either.

This is the array which is generated by the csv that has been made by ms office on windows machine which works fine.

Array
(
[0] => Array
    (
        [0] => Student Name
        [1] => Student Roll
        [2] => Class
        [3] => Section
        [4] => Exam
        [5] => Subject
        [6] => Total Marks
        [7] => Grade
        [8] => Objective
        [9] => Subjective
        [10] => Practical
    )

[1] => Array
    (
        [0] => Sample Name
        [1] => 123
        [2] => Nine
        [3] => A
        [4] => Mid Term
        [5] => Math
        [6] => 80
        [7] => A+
        [8] => 40
        [9] => 20
        [10] => 20
    )

)

But in linux my csv format is not working correctly. It's pushing all the data into one key & one element like below.

Array
(
[0] => Array
    (
        [0] => Student Name Student Roll    Class   Section Exam    Subject Total Marks Grade   Objective   Subjective  Practical
    )

[1] => Array
    (
        [0] => Samp0le Name 123 Nine    A   Mid Term    Math    80  A+  40  20  20
    )

)

So my actual question is how can I get read of that?

This is my coding for the upper array generation:

$csv = array();
    //$file = fopen('myCSVFile.csv', 'r');
    ini_set("auto_detect_line_endings", true);
    while (($result = fgetcsv($fp)) !== false)
    {
        $csv[] = $result;
    }

    fclose($fp);

    echo '<pre>';
    print_r($csv);
    echo '</pre>';

Please help me I'm in very need actually ..

解决方案

Try This

$file = "FNAC_SPECTACLES.csv";
$handle = fopen($file, "r");
$row = 1;
while (($data = fgetcsv($handle, 0, ";","'")) !== FALSE) 
{
    if($row == 1)
    {
        // skip the first row   
    }
    else
    {
        echo "<pre>";
        print_r($data);
    }
    $row++;
}

fgetcsv() in third argument in separator to you want to save in csv file. Ex: comma,semicolon or etc..

这篇关于fgetcsv没有正确读取csv文件,保存在linux系统中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆