gpt4 book ai didi

php - fgetcsv 没有正确读取保存在 linux 系统中的 csv 文件

转载 作者:太空宇宙 更新时间:2023-11-04 11:11:21 24 4
gpt4 key购买 nike

我正在尝试通过 csv 文件将一些数据导入我的数据库。问题是在开发之初我使用的是 windows,所以我的 csv 文件是在 ms office 上编写的。所以在那里导入时它工作正常。但是在保存 csv 文件时切换到 linux(UBUNTU) 它没有按我想要的那样显示数据。我也没有得到任何解决方案。

这是由 ms office 在 windows 机器上制作的 csv 生成的数组,工作正常。

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
)

)

但在 Linux 中,我的 csv 格式无法正常工作。它将所有数据推送到一个键和一个元素中,如下所示。

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
)

)

所以我的实际问题是我怎样才能读到它?

这是我对上层数组生成的编码:

$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)
{
// skip the first row
}
else
{
echo "<pre>";
print_r($data);
}
$row++;
}

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

关于php - fgetcsv 没有正确读取保存在 linux 系统中的 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com