Public.Standards.CSV

[ Start > PikeModules > Module > Public.Standards.CSV ] [ Edit this Page | Viewing Version 2 ]


This module was made following http://en.wikipedia.org/wiki/Comma-separated_values

The examples shown below use the sample CSV file provided by the fore-mentionned documentation.

Files

Public.Standards.CSV.FILE() inherits from the buffered Stdio.FILE() class.

It sports two important methods:

  • int write_row(mixed… row)
  • int|array read_row()

Reading

The method-way:

object infile = Public.Standards.CSV.FILE("/tmp/sample.csv", "r");
infile->read_row();
(1) Result: ({ /* 5 elements */
                "1997",
                "Ford",
                "E350",
                "ac, abs, moon",
                "3000.00"
            })

The iterator-way:

object infile = Public.Standards.CSV.FILE("/tmp/sample.csv", "r");
foreach(infile; int line_number; array data) {
//...
}

Writing

object outfile = Public.Standards.CSV.FILE("/tmp/foo.csv", "cw");
outfile->write_row("foo","bar","baz");
(1) Result: 13

String

Public.Standards.CSV.CSVString() deals with string.

As a side note, the name Public.Standards.CSV.String() was originally chosen for consistency, but produced unexpected results i didn't managed to overcome. Anyone with a clue here is welcomed.

Public.Standards.CSV.CSVString() work the very same way than Public.Standards.CSV.FILE()

Reading

The method way:

object data = Public.Standards.CSV.CSVString(Stdio.read_bytes("/tmp/sample.csv"));
data->read_row();
(3) Result: ({ /* 5 elements */
                "1997",
                "Ford",
                "E350",
                "ac, abs, moon",
                "3000.00"
            })

The iterator way:

object instring = Public.Standards.CSV.CSVString(Stdio.read_bytes("/tmp/sample.csv"));
foreach(instring; int line_number; array dara) { 
// ...
}

Writing

Public.Standards.CSV.CSVString() doesn't support writing for now.


Powered by PikeWiki2

 
gotpike.org | Copyright © 2004 - 2009 | Pike is a trademark of Department of Computer and Information Science, Linköping University