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 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解