Solved

CSv Unique column A data while adding up column b data

Posted on 2009-05-04
6
199 Views
Last Modified: 2013-12-13
I have to 2 columns of information in a CSV file example

Column A       Column B (stats)

pink                      2
pink                      3
red                       1
red                       6
red                       3
black                    1
black                    5
black                    2

the results I am after are Column A. unique words only but at the same time to add up the stats with matching keyword stats

The results should be

pink                5    
red                 10
black              8

If it could be written as another CSV file it would be appeciated
0
Comment
Question by:danialbb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 2

Expert Comment

by:lavinpj1
ID: 24297355
Hello,

Can you post an example of the CSV format you're working with? I assume it's something like:

pink,2
pink,3
red,1
red,6

but clarification would be handy.

Phil
0
 

Author Comment

by:danialbb
ID: 24297382
Yes thats the format

Column A , column B

pink,1
pink,3

etc
0
 
LVL 2

Expert Comment

by:lavinpj1
ID: 24297648
<?php
$filename = '/home/phil/file.csv';

// Read the file
$handle = fopen($filename, 'r');
$contents = trim(fread($handle, filesize($filename)));

// Array for output
$output = array();

// Split by \n and loop
$lines = explode("\n", $contents);

foreach ($lines as $line) {
      // Split by , and enumerate
      $line = rtrim($line);
      $temp = explode(',', $line);

      $output[$temp[0]] += $temp[1];
}

// Loop and output
foreach ($output as $key=>$val) {
      echo "{$key},{$val}\n";
}
?>

That reads from a file and outputs it to the page/console. Let me know if you had other intentions for input/output.

Phil
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:danialbb
ID: 24297679
Could you write to a csv file please. And I will accept the answer once tested.

Thanks for this.
0
 
LVL 2

Accepted Solution

by:
lavinpj1 earned 500 total points
ID: 24297732
<?php
$inFilename = '/home/phil/file.csv';
$outFilename = '/home/phil/outfile.csv';

// Read the file
$handle = fopen($inFilename, 'r');
$contents = trim(fread($handle, filesize($inFilename)));
fclose($handle);

// Array for output
$output = array();

// Split by \n and loop
$lines = explode("\n", $contents);

foreach ($lines as $line) {
      // Split by , and enumerate
      $line = rtrim($line);
      $temp = explode(',', $line);

      $output[$temp[0]] += $temp[1];
}

// Open output file
$handle = fopen($outFilename, 'w+');

// Loop and output
foreach ($output as $key=>$val) {
      fwrite($handle, "{$key},{$val}\n");
}

fclose($handle);
?>


Phil
0
 

Author Closing Comment

by:danialbb
ID: 31577647
Excellent work Thanks Again
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question