14
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NodeでSJISのCSVを読み込む

Posted at

Windows環境向けに出力されたCSVって大抵SJISですよね。
まぁSJISならダブルクリックでエクセルで開けて便利ですからね。
で、それをNodeで読み込む際にはUTF8にする必要があるのでその方法。

$ node -v
v6.2.2
$ npm install csv iconv-lite
$ npm ls
├─┬ csv@1.1.0
│ ├── csv-generate@1.0.0
│ ├── csv-parse@1.1.1
│ ├─┬ csv-stringify@1.0.4
│ │ └─┬ lodash.get@4.3.0
│ │   └─┬ lodash._stringtopath@4.8.0
│ │     └── lodash._basetostring@4.12.0
│ └── stream-transform@0.1.1
└── iconv-lite@0.4.13
csv_sample.js
var fs = require('fs');
var iconv = require('iconv-lite');
var csv = require('csv');

var rs = fs.createReadStream('sample.csv')
    .pipe(iconv.decodeStream('SJIS'))
    .pipe(iconv.encodeStream('UTF-8'))
    .pipe(csv.parse())
    .pipe(csv.transform(function(record) {
      console.log(record);
    }));
14
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?