dongyi1215 2017-07-14 04:48
浏览 76
已采纳

我想读取csv文件并将其解析为数组但总是失败

my csv file link : https://drive.google.com/file/d/0B-Z58iD3By5wb2R2TnV0Rjc3Zzg/view

I already read many reference but I can not seperate my csv by "," (the delimiter not working properly). Is there a solution how could I get an array like this from that csv file:

`Array[0]=>
(

['username'] => Lexsa,

['date'] => 12/07/2017,

['retweet'] => null,

)`

`Array[1]=>
 (

 ['username'] => any,
 ['date'] => 12/07/2017,

 ['retweet'] => null
 )`



function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
    return FALSE;

$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
    while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
    {
        if(!$header)
            $header = $row;
        else
            $data[] = array_combine($header, $row);
    }
    fclose($handle);
}
return $data;
} 

I try to use many reference but the result is always like this the code wont split the line with "," :

Array ( [0] => Array (["username","date","retweets","favorites","text","geo","mentions","hashtags","id","permalink"] => "Lexsa911","01/12/2016 0:05",0.0,0.0,"Kecelakaan - Kecelakaan Pesawat yang Melibatkan Klub-Klub Sepakbola http:// ht.ly/1IdL306EzDH",,,,"8,04E+17","https://twitter.com/Lexsa911/status/804008435020865536" )

  • 写回答

2条回答 默认 最新

  • doulan9287 2017-07-14 05:48
    关注

    This is what I get when I open your tes.csv with less or gedit:

    """username"",""date"",""retweets"",""favorites"",""text"",""geo"",""mentions"",""hashtags"",""id"",""permalink"""
    """Lexsa911"",""01/12/2016 0:05"",0.0,0.0,""Kecelakaan - Kecelakaan Pesawat yang Melibatkan Klub-Klub Sepakbola http:// ht.ly/1IdL306EzDH"",,,,""8,04E+17"",""https://twitter.com/Lexsa911/status/804008435020865536"""
    """Widya_Davy"",""01/12/2016 0:05"",0.0,0.0,""Kecelakaan - Kecelakaan Pesawat yang Melibatkan Klub-Klub Sepakbola http:// ow.ly/h1Eh306EzHk"",,,,""8,04E+17"",""https://twitter.com/Widya_Davy/status/804008434588876803"""
    """redaksi18"",""01/12/2016 0:05"",0.0,0.0,""Klub Brasil Korban Kecelakaan Pesawat Didaulat Jadi Juara http:// beritanusa.com/index.php?opti on=com_content&view=article&id=39769:klub-brasil-korban-kecelakaan-pesawat-didaulat-jadi-juara&catid=43:liga-lain&Itemid=112 … pic.twitter.com/1K7OlZSX83"",,,,""8,04E+17"",""https://twitter.com/redaksi18/status/804008416188338176"""
    """JustinBiermen"",""01/12/2016 0:06"",0.0,0.0,""Video LUCU Kecelakaan Yg Sangat Koplak http://www. youtube.com/watch?v=pQFOY7 AdXck …"",,,,""8,04E+17"",""https://twitter.com/JustinBiermen/status/804008714738880512"""
    

    So the issue is not the delimiter, but rather the enclosure. As you can see, each line is wrapped in quotes. So the entire line is considered to be a single column.

    I suggest to fix the csv, e.g. remove the quotes until a row looks like

    "username","date","retweets","favorites","text","geo","mentions","hashtags","id","permalink"
    

    If you cannot do that for some reason, preprocess the csv to clean it up:

    print_r(
        array_map(
            function($line) {
                $single_quoted_line = str_replace(['"""', '""'], '"', $line);
                return str_getcsv($single_quoted_line);
            },
            file("tes.csv")
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥60 SOL语句中Where查询中的 from to 语句能不能从小到大换成从大到小(标签-SQL)
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))