Permalink
Browse files

example of use + an equivalent example based on perl's Text::CSV

  • Loading branch information...
1 parent cd9515d commit 7f64b04e454f32dce788a59c628f8e2a9148c7f5 @koles koles committed Jul 9, 2010
Showing with 56 additions and 0 deletions.
  1. +32 −0 perl-csv-test.pl
  2. +24 −0 test.js
View
@@ -0,0 +1,32 @@
+#!/usr/bin/perl -w
+
+use Text::CSV_XS;
+
+my %CSV_OPTS_IN = (
+ 'binary' => 1,
+ 'quote_char' => '"',
+ 'escape_char' => '"',
+);
+my %CSV_OPTS_OUT = %CSV_OPTS_IN;
+$CSV_OPTS_OUT{'always_quote'} = 1;
+
+my $CSV_IN = Text::CSV_XS->new(\%CSV_OPTS_IN) or die "" . Text::CSV_XS->error_diag;
+
+sub main() {
+ my $start = localtime;
+ print "$start\n";
+ my $fh = *STDIN;
+
+ until ($CSV_IN->eof) {
+ my $hr = $CSV_IN->getline($fh) or do {
+ last if $CSV_IN->eof;
+ die "CSV_IN getline error: " . Text::CSV_XS->error_diag;
+ };
+ my $a = 0;
+ }
+
+ my $end = localtime;
+ print "$end\n";
+}
+
+main();
View
24 test.js
@@ -0,0 +1,24 @@
+var csv = require('csv'),
+ sys = require('sys');
+
+sys.log('start');
+
+if (process.argv.length < 3) {
+ sys.error("Usage: node " + process.argv[1] + " <csv file>");
+ process.exit(1);
+}
+
+var file = process.argv[2];
+
+var csvIn = csv.createCsvFileReader(file, {
+ 'separator': ':',
+ 'comment': '#',
+});
+var csvOut = new csv.CsvWriter(process.stdout);
+
+csvIn.addListener('end', function() {
+ sys.log('end');
+});
+csvIn.addListener('data', function(data) {
+ csvOut.writeRecord(data);
+});

0 comments on commit 7f64b04

Please sign in to comment.