[CALUG] Reading CSV using Python / PHP

Bryan Smith b.j.smith at ieee.org
Thu Apr 28 11:37:27 EDT 2016


Perl [1] and Python [2], or Ruby for that matter, have CSV modules/methods.

I refuse to enter programming language debates, and have always said
"my favorite language is the one the client can maintain after I've
been hit by a bus."

Perl's C-based string manipulation routines are fast, while Python's
strong typeset and object oriented nature make for some of the most
maintainable, and capable, code.  Although I often joke that "Ruby is
the new Perl," where Perl was designed before the Web, Ruby more as a
result of it.

In either case,  I wouldn't get too deep into creating CSV code, and
leverage those existing Perl and Python CSV modules/methods.  I'm a
long-time Perl hacker (pre-Web, even Perl::Win32 in early NT 3.x
days), but when dealing with methods and objects, I prefer to code in
Python as Perl gets quite mess.

I.e., my rule-of-thumb for myself has been ...
 - When I deal with parsing single lines, bash w/sed
 - When I deal with parsing multiple lines, perl
 - When I deal with parsing using methods/objects, python

When I do CSV files, XMLRPC or SOAP calls/return values -- especially
including hashes and hashes of arrays and arrays of hashes and arrays
of hashes of arrays -- I'm using Python.  ;)

So if I was teaching you how to programmatically manage CSV files, I'd
start with Python methods and its object reference approach. [2b]

But Perl and Ruby are just as capable.

-- bjs

P.S.  Also keep in mind that there can be codepage issues, especially
with older Perl.  E.g., lack of support other than for UTF-8.

[1] http://search.cpan.org/~makamaka/Text-CSV-1.33/lib/Text/CSV.pm

[2a] https://docs.python.org/2/library/csv.html

[2b] http://www.pythonforbeginners.com/systems-programming/using-the-csv-module-in-python/


On Thu, Apr 28, 2016 at 11:11 AM, Tim Spangler <tspang at jefnet.com> wrote:
> You could always use Perl – file manipulation is Perl’s bread and butter.
> Cpan shows over 500 CSV modules (https://metacpan.org/search?q=csv), one of
> which should do what you need.
>
>
>
> From: CALUG [mailto:calug-bounces at unknownlamer.org] On Behalf Of Rajiv Gunja
> Sent: Thursday, April 28, 2016 10:37 AM
> To: CALUG
> Subject: [CALUG] Reading CSV using Python / PHP
>
>
>
> Hey all, I am trying to cut down on the amount of manual steps I have to
> take editing CSV file - sorting, creating new tabs and categorising them.
>
> I googled and found logparser from MS, could not figure it out - maybe a
> mental block towards anything MS.
>
>
>
> I saw that it was possible using python or PHP. So turning to you guys for
> advice. Which one works best or is there a different way?
>
>
>
> I do not know Python, but I can learn. PHP, I am very familiar with, but
> whichever is efficient and can do a better job, I will use it.
>
>
>
> Problem:
>
> 1. CSV file has 2000+ rows
>
> 2. Some columns are multi-line with \r and \n
>
> 3. fields are separated by "," and each data item is between quotes (" ")
>
>
>
> Please let me know.
>
>
>
> -GG
>
>
> _______________________________________________
> CALUG mailing list
> CALUG at unknownlamer.org
> http://lists.unknownlamer.org/listinfo/calug
>



-- 

--
Bryan J Smith  -  http://www.linkedin.com/in/bjsmith
E-mail:  b.j.smith at ieee.org  or  me at bjsmith.me



More information about the CALUG mailing list