csv library - a project I may actually finish
Posted: Fri Feb 15, 2008 6:57 pm
I'm putting together a library to read and write csv files. I realize this is a pretty trivial task, but I've added some neat features. As far as design goes, I borrowed heavily from python's csv module. Here are some things that are already possible with the code I've written:
the simplest possible way to read a csv file
A few other examples
I'm just looking for advice on how to improve the API, what features would be useful etc. If you guys would like to see the code, I'll open the svn repo to the public.
A few things I'm considering:
providing a $reader->output() to force download (it would output the correct headers)
allowing Csv_Dialect->$quoting = Csv_Dialect::QUOTING_CUSTOM and then Csv_Reader::$quotecolumns = array(0,3,5,6)
I'm also like to allow the user to provide a mapping class to the reader object so that you could map columns to names and callback functions:
But I can't find a syntax I like.
the simplest possible way to read a csv file
Code: Select all
$reader = new Csv_Reader('/path/to/myfile.csv');
foreach ($reader as $row) {
// do something.. $row now contains an array of the current row
}Code: Select all
$reader = new Csv_Reader('/path/to/myfile.csv');
echo count($reader); // outputs total lines
echo $reader->count(); // does the same thing
$header = $reader->current(); // grabs first row
while ($row = $reader->current()) { // starts from second row
// do something
}
$dialect = new Csv_Dialect_Excel;
$reader = new Csv_Reader('/path/to/file', $dialect); // now reader will read excel csv file properly (this does not mean xls)
foreach ($reader as $row) {
// do something
}
$dialect->delimiter = "\t";
$writer = new Csv_Writer('/path/to/file', $dialect);
foreach ($reader as $row) {
$writer->writeRow($row);
}
// or you can do this
// $writer->writeRows($reader);
$writer->close(); // writes csv file but now it's tab-delimited
try {
$reader = new Csv_Reader('/path/to/file');
} catch (Exception $e) {
// could not open file
}
$dialect = new Csv_Dialect;
$dialect->quoting = Csv_Dialect::QUOTE_NONNUMERIC; // options are listed here: http://docs.python.org/lib/csv-contents ... v-contents
$dialect->delimiter = ";";
$dialect->lineterminator = "\r";
$dialect->quotechar = "'";
$dialect->escapechar = "\\";
$dialect->skipblanklines = false;
$reader = new Csv_Reader('/path/to/file', $dialect); // now this reader will read according to $dialect
$writer = new Csv_Writer('/path/to/file', $dialect); // now this writer will write according to $dialectA few things I'm considering:
providing a $reader->output() to force download (it would output the correct headers)
allowing Csv_Dialect->$quoting = Csv_Dialect::QUOTING_CUSTOM and then Csv_Reader::$quotecolumns = array(0,3,5,6)
I'm also like to allow the user to provide a mapping class to the reader object so that you could map columns to names and callback functions:
Code: Select all
$mapper = new Csv_Mapper();
$mapper->map(0, 'id'); // maps first column to id
$mapper->map(2, 'name');
$filter = new Csv_Filter();
$filter->addFilter('id', new Csv_Filter_digits());
$filter->addFilter('name', new Csv_Filter_ucWords());
$filter->addFilter('name', new Csv_Filter_alnum(true)); // argument == allowWhitespace
$reader = new Csv_Reader('/path/to/file');
$reader->addMapper($mapper);
$reader->addFilter($filter);
foreach($reader as $row) {
printf("Your name is %s and your id is %d", $row['name'], $row['id']);
}