Programming Languages war Python, PHP, Javascrip (Nodejs) Read CSV file

Hey my friends, a new post about this war. I don't know which is better, but I'll try to figure it out. I hope y'all enjoy this series. Here we go!

It's regular in the job environment to have to read csv files, to get any information or to pass this info to database.

For this example, you need to create a file called "test.csv" with 3 columns and how many rows do you want.

Python

It's necessary import the library "csv" this library has methods that you can use for reading all CSV files, it's matter to use the method "open" that we saw three lessons ago,then to read the csv file, you have to use "csv.reader" with two parameters, first file opened, second the delimiter, and next this file you can separate into columns like a list, this list has two dimensions, then you can read line by line whole document and each line has a list with all columns separately, the first line is the header that you give to your columns in the CSV file.

import csv

with open('test.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
        else:
            print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')

        line_count += 1
    print(f'Processed {line_count} lines.')

PHP

You need to call the function "fopen". This function is for to open document, next you have to read each line with a "while", into "while" you use the function "fgetcsv" with three parameters, the file opened, how many lines do you want to read, if you pass 0 is the ilimited or must be greater than the longest line (in characters) to be found in the CSV file, at last you pass the delimiter, the first line is the header that you give to your columns in the CSV file.

<?php
$line_count = 0;
if (($manager = fopen("test.csv", "r")) !== false) {
    while (($data = fgetcsv($manager, 0, ",")) !== false) {
        if ($line_count === 0) {
            $header = implode(",", $data);
            echo "Column names are $header \n";
        } else {
            echo "$data[0] works in the $data[1] department, and was born in $data[2].\n";
        }
        $line_count++;
    }
    fclose($manager);
}
echo "Processed $line_count lines.";

?>

Update

A friend of mine, Juan Millan recommends me a code in PHP, thank you for that, he created a function to receive the file path and the length of the line, next he opens the file and reads line by line, this way is more efficient in the use of data memory but a little slower, bellow you can see the stats.

function readCSV(String $filePath, Int $length = 4096)
{
    $file = fopen($filePath, 'r');

    while (!feof($file)) {
        $row = fgetcsv($file, $length, ",");

        yield $row;
    }
}

foreach (readCSV('test.csv') as $row) {
    # Add logic to render here!
    echo implode(",", $row);
    echo PHP_EOL;
}

Javascript

You need to import the method "fs" this method is important to manage the files, So this method has the function "readFile". You have to use the method "csv-parser", this method has methods that you can use for read all csv file, first you read the file with "fs.createReadStream", it's matter to pass a "pipe" function, what is the "pipe" function? it's the way like you want to manipulate this information, in this case I pass de csv method, then here you have to get separated, headers and data, and this data is an object.

const csv = require('csv-parser');
const fs = require('fs');
const results = [];
let line_count = 0;

fs.createReadStream('test.csv')
  .pipe(csv({ separator: ',' }))
  .on('headers', headers => {
    console.log(`Column names are ${headers}`);
  })
  .on('data', data => {
    console.log(
      `${data.nombre} works in the ${data.apellido} department, and was born in ${data.edad}`
    );
    line_count++;
  })
  .on('end', () => {
    console.log(`Processed ${line_count} lines.`);
  });

Conclusion

  • Structure: Python and PHP are very similar, change a little for the braces and functions' name, but javascript is different because you have to know how javascript use asynchronism.
  • Lines: Python: 13, PHP: 17, Javascript: 19
  • Time to execution: For this test I use a file of 45.9 MB, and I ran 3 times. Python: 2.982s, PHP: 9.234s, other PHP: 9.72s, Javascript: 6.533s

Would you like that I write about more info about CSV functions?

I hope you enjoy my post and remember that I am just a Dev like you!

No Comments Yet