Jump to content

Im confused with large csv reading line by line


Go to solution Solved by .josh,

Recommended Posts

i have to read a single line from a csv,  its a really big file and i only need one column.

i need the response to be a string ,i made a search and found the following code but i dont have any idea how to get a single line from a single string per run .

<?php
$row = 1;
//open the file
if (($handle = fopen("file.csv", "r")) !== FALSE)
{
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE)
    {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++)
        {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>
Edited by bores_escalovsk

if you need to find something specific, wrap it with an if statement, perform any search or checks on it and then echo or save as a variable.

 

examples:

if($data[$c] != ''){
$string = $data[$c];
echo $string . "<br />\n";
}

or if to make them a new array

$new_array = array();//outside the loop


if($data[$c] != ''){
$new_array[] = $data[$c];
}
Edited by QuickOldCar

what are you trying to do... are you trying to just echo out the value of only 1 column? Are you trying to put it into a db?

its a part of my code , the echo is only for testing ...the code needs to read 1 line per run ,return the value as a string and at the next run read the next line.

this is worst than the time i learned regex ,-,

 

If it's a large file you should save the csv to a database and query each result that way

Having to load a very large file each time takes lots of resources.

 i was trying to do that, but the file have 2gb , not the easyest thing

this is worst than the time i learned regex ,-,

I don't mean to be offensive but regex is one of the hardest things to master (in any language). If you managed that then I'm not sure how a couple of simple loops is troubling you...

this is worst than the time i learned regex ,-,

 

 i was trying to do that, but the file have 2gb , not the easyest thing

Split the csv file up then try it

http://sourceforge.net/projects/splitcsv/

Split the csv file up then try it

http://sourceforge.net/projects/splitcsv/

just for the record , i solved by using dbforge and transforming the csv in a mysql table, way easier than what i was trying to do. thank you all guys

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.