我需要在一个CSV列得到的值(分组)的计数 [英] I need to get a count of values (grouped) in a csv column

查看:145
本文介绍了我需要在一个CSV列得到的值(分组)的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到的第一列的值的计数。这些ID的可能或不可能在我收到任何给定的.csv文件存在。所以,我需要遍历.csv文件看第一列,要么将它添加到保持阵列($ PWS有关),如果它不存在或递增计数该保持数组,如果我已经添加了。

我必须使用第一循环fgetcsv()。这适用于裂解成文件:

  $ PWS有关=阵列();$ handle2 =的fopen($ uploadfileandpath,R);
而($ field2array = fgetcsv($ handle2,130000,,))
{
    //这里是我想补充的价值或增值$ PWS有关阵列
    而(?)
    {
        如果($ field2array [0]!=?)
        {
            //添加或增量
        }
    }
}

下面是实际数据。第一列具有公共供水系统的ID。我需要指望他们。

 00513,2009年8月13日,090834311A,R,4,OR1000x6,N,N,E ,, 1,73,COLILERT
00513,2009年8月13日,090834312A,R,39,OR1000x6,N,N,E,, 1,35,COLILERT
00154,2009年8月13日,090835401A,R,瀑布路300,OR100016,N,N,E,, 1。10, COLILERT
95343,2009年8月13日,090835601A,R,1房汇,OR1000x6,N,N,E,, 1 ,,COLILERT
94585,2009年8月14日,090837701A,R,厨房,OR1000x6,N,N,E,, 1 ,,COLILERT
94704,2009年8月14日,090837801A,R,外塔,OR1000x6,N,N,E,, 1 ,,COLILERT
01430,2009年8月14日,090838201A,R,百鹿园LN OT,OR1000x6,N,N,E,, 1 ,,COLILERT
00625,2009年8月14日,090839001A,R,达诺和N玫瑰,OR100016,N,N,E,, 1,35 COLILERT
00405,2009年8月17日,090840301A,R,韦斯特蒙特驱动器,OR100016,N,N,E,, 1,28, COLILERT
01031,2009年8月17日,090840401A,R,单元2龙头,OR100016,N,N,E,, 1 ,,COLILERT
00625,2009年8月17日,090840601A,R,Luman路,OR1000x6,N,N,E,, 1,35, COLILERT
00513,2009年8月17日,090841001A,R,40,OR1000x6,N,N,E,, 1,18,COLILERT
00513,2009年8月17日,090841002A,R,10,OR1000x6,N,N,E,, 1,16,COLILERT


解决方案

  $跳频=的fopen('FILE.CSV','RB');$ PWS =阵列();
而($行= fgetcsv($ FH)){
    $ PWS [$行[0] ++;
}

基本上,它会使用第一列值作为键填充PWS,并增加他们,因为他们一起走。此后,鉴于上述样品CSV,你最终以

  $ PWS =阵列(
    '00513'=> 4
    '00154'=> 1
    '95343'=> 1
    '94585'=> 1
等等...
);

I need to get a count of the first column's values. These ID's might or might not exist in any given .csv file I receive. So I need to loop through the .csv file looking at the first column and either adding it to a holding array ($PWSs) if it doesn't exist or incrementing the count in this holding array if I've already added it.

I have the first loop using fgetcsv()..this works for cracking into the file:

$PWSs = array();

$handle2 = fopen ($uploadfileandpath,"r");
while ($field2array = fgetcsv ($handle2, 130000, ",")) 
{
    // Here is where I would add value or increment $PWSs array
    while (?)
    {
        if ($field2array[0] != ?)
        {
            // Add or increment
        }
    }
}

Here is actual data. The first column has IDs for Public Water Systems. I need to count them.

"00513","08/13/2009","090834311A","R","4","OR1000x6","N","N","E",,1,".73","COLILERT"
"00513","08/13/2009","090834312A","R","39","OR1000x6","N","N","E",,1,".35","COLILERT"
"00154","08/13/2009","090835401A","R","300 Falls Road","OR100016","N","N","E",,1,".10","COLILERT"
"95343","08/13/2009","090835601A","R","Room 1 Sink","OR1000x6","N","N","E",,1,,"COLILERT"
"94585","08/14/2009","090837701A","R","Kitchen","OR1000x6","N","N","E",,1,,"COLILERT"
"94704","08/14/2009","090837801A","R","Outside Tap","OR1000x6","N","N","E",,1,,"COLILERT"
"01430","08/14/2009","090838201A","R","100 Deer Park Ln OT","OR1000x6","N","N","E",,1,,"COLILERT"
"00625","08/14/2009","090839001A","R","Dano and N Rose","OR100016","N","N","E",,1,".35","COLILERT"
"00405","08/17/2009","090840301A","R","Westmont Drive","OR100016","N","N","E",,1,".28","COLILERT"
"01031","08/17/2009","090840401A","R","Unit 2 Faucet","OR100016","N","N","E",,1,,"COLILERT"
"00625","08/17/2009","090840601A","R","Luman Road","OR1000x6","N","N","E",,1,".35","COLILERT"
"00513","08/17/2009","090841001A","R","40","OR1000x6","N","N","E",,1,".18","COLILERT"
"00513","08/17/2009","090841002A","R","10","OR1000x6","N","N","E",,1,".16","COLILERT"

解决方案

$fh = fopen('file.csv', 'rb');

$PWS = array();
while($row = fgetcsv($fh)) {
    $PWS[$row[0]]++;
}

Basically it'll populate the PWS using the first column values as keys, and increment them as they come along. Afterwards, given your sample csv above, you'd end up with

$PWS = array(
    '00513' => 4
    '00154' => 1
    '95343' => 1
    '94585' => 1
etc...
);

这篇关于我需要在一个CSV列得到的值(分组)的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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