parsing_csv

Parsing CSV files

In this tutorial you will learn how to parse a simple CSV (comma separated values) file. This is the sort of file produced by spreadsheets and other programs
when a text-only, portable format is required for exporting data.

The file we will use in the examples below is a text file called
prospects.csv, and was produced by the OpenOffice.org ‘calc’ spreadsheet.

The format is the same as that produced by any program that claims
to produce a valid CSV file, namely the field delimiter (the thing that
separates the fields), is a comma, and textual data is surrounded by
double quote characters. Numeric data is typically not quoted.

Text::CSV

The Text::CSV module provides functions for both parsing and producing CSV data. However, we’ll focus on the parsing functionality here. The following code
sample opens the prospects.csv file and parses each line in turn, printing
out all the fields it finds.

Running the code produces the following output:

And by replacing the line:

with:

we can get more particular about which fields we want to output. And while we’re
at it, let’s skip past the first line of our csv file, since it’s only a list
of column names.

Running this code will give us the following output:

Tie::CSV_File

You can use this module to manipulate a csv file as an array of arrays.
For instance to access a field on a specific row, you might write:

which produces:

Tie::Handle::CSV

If you’d prefer to access the fields in your CSV file by name, use
Tie:Handle::CSV. Here is some code that displays the Contact and Address field from each data row in the csv file.

This produces:

DBD::CSV

And now for a slightly surreal twist, (and a demonstration of some of the
genuinely novel functionality you’ll find on cpan), let’s finish with an
examination of the DBD::CSV module, which allows us to access data in a
csv file with SQL statements. A significant subset of SQL is supported
by this module, it’s well worth looking at.

When run, the code above produces this output:

Now for an example that manually overrides the columns names in our table
with names of our choosing.

which produces:

See also

Scroll to Top