<commit_before>---
title: CSV Parse Examples
layout: page
tags: ['intro','page']
pageOrder: 1
github: 'https://github.com/adaltas/node-csv-parse'
---

## Using the callback API

The parser receives a string and returns an array inside a user-provided
callback. This example is available with the command `node samples/callback.js`.

```javascript
var parse = require('csv-parse');
require('should');

var input = '#Welcome\n"1","2","3","4"\n"a","b","c","d"';
parse(input, {comment: '#'}, function(err, output){
  output.should.eql([ [ '1', '2', '3', '4' ], [ 'a', 'b', 'c', 'd' ] ]);
});
```

## Using the stream API

The CSV parser implements the [`stream.Transform`API][stream_transform].

CSV data is sent through the `write` function and the resulting data is obtained
within the "readable" event by calling the `read` function. This example is
available with the command `node samples/stream.js`.

```javascript
var parse = require('csv-parse');
require('should');

var output = [];
// Create the parser
var parser = parse({delimiter: ':'});
// Use the writable stream api
parser.on('readable', function(){
  while(record = parser.read()){
    output.push(record);
  }
});
// Catch any error
parser.on('error', function(err){
  console.log(err.message);
});
// When we are done, test that the parsed output matched what expected
parser.on('finish', function(){
  output.should.eql([
    [ 'root','x','0','0','root','/root','/bin/bash' ],
    [ 'someone','x','1022','1022','a funny cat','/home/someone','/bin/bash' ]
  ]);
});
// Now that setup is done, write data to the stream
parser.write("root:x:0:0:root:/root:/bin/bash\n");
parser.write("someone:x:1022:1022:a funny cat:/home/someone:/bin/bash\n");
// Close the readable stream
parser.end();
```

## Using the pipe function

One useful function of the Stream API is `pipe` to interact between
multiple streams. You may use this function to pipe a `stream.Readable` string<commit_msg>example: use end event instead of finish<commit_after>---
title: CSV Parse Examples
layout: page
tags: ['intro','page']
pageOrder: 1
github: 'https://github.com/adaltas/node-csv-parse'
---

## Using the callback API

The parser receives a string and returns an array inside a user-provided
callback. This example is available with the command `node samples/callback.js`.

```javascript
var parse = require('csv-parse');
require('should');

var input = '#Welcome\n"1","2","3","4"\n"a","b","c","d"';
parse(input, {comment: '#'}, function(err, output){
  output.should.eql([ [ '1', '2', '3', '4' ], [ 'a', 'b', 'c', 'd' ] ]);
});
```

## Using the stream API

The CSV parser implements the [`stream.Transform`API][stream_transform].

CSV data is sent through the `write` function and the resulting data is obtained
within the "readable" event by calling the `read` function. This example is
available with the command `node samples/stream.js`.

```javascript
var parse = require('csv-parse');
require('should');

var output = [];
// Create the parser
var parser = parse({delimiter: ':'});
// Use the writable stream api
parser.on('readable', function(){
  while(record = parser.read()){
    output.push(record);
  }
});
// Catch any error
parser.on('error', function(err){
  console.log(err.message);
});
// When we are done, test that the parsed output matched what expected
parser.on('end', function(){
  output.should.eql([
    [ 'root','x','0','0','root','/root','/bin/bash' ],
    [ 'someone','x','1022','1022','a funny cat','/home/someone','/bin/bash' ]
  ]);
});
// Now that setup is done, write data to the stream
parser.write("root:x:0:0:root:/root:/bin/bash\n");
parser.write("someone:x:1022:1022:a funny cat:/home/someone:/bin/bash\n");
// Close the readable stream
parser.end();
```

## Using the pipe function

One useful function of the Stream API is `pipe` to interact between
multiple streams. You may use this function to pipe a `stream.Readable` string
{'fraction_non_alphanumeric': 0.18388746803069053, 'fraction_numerical': 0.014833759590792838, 'mean_word_length': 4.709489051094891, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7470105', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1205, 'n_words': 399}