csv cleaning reading writting

Discussion in 'PHP' started by arunsuriindia, Mar 23, 2007.

  1. #1
    NET_TURNOV TDCLOINDI
    290418
    256124257
    1047126
    red 8966
    513168
    162230
    red 2669

    this the conntent of my txt file,

    now i want to read this file in php
    delete column 2
    also delete all rows having red

    and saved as csv

    my approach
    i use fopen($filename,'r');
    then i have no clue how to do this job.
     
    arunsuriindia, Mar 23, 2007 IP
  2. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #2
    best approach is to load as file($FILE) to put the contents into an array

    then depending whether your csv is a tab, comma etc recurse over the array to do what you need to
     
    Big 'G', Mar 23, 2007 IP
  3. arunsuriindia

    arunsuriindia Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    could you just write a bit of code i am a new bie not having able to understand it well
     
    arunsuriindia, Mar 23, 2007 IP
  4. arunsuriindia

    arunsuriindia Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ya how do i write the array back with commas
     
    arunsuriindia, Mar 23, 2007 IP
  5. voodoo709

    voodoo709 Member

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #5
    Maybe you could try something like this:



    
    
    
    $lines = file('test_file.txt');
    
    $content='';
    
    foreach($lines as $row){
    
    $temp = explode(' ',$row);
    
    	if($temp[0] !='red'){
    	$content .=$temp[0];
    	}
    }
    
    file_put_contents('out.txt',$content);				//PHP 5 function
    
    PHP:
    The output would be

    NET_TURNOV
    290418
    256124257
    1047126
    513168
    162230




    It’s probably not the best way of doing it but it will work :D
     
    voodoo709, Mar 23, 2007 IP