Skip to content

Instantly share code, notes, and snippets.

@hanksudo
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanksudo/8701495 to your computer and use it in GitHub Desktop.
Save hanksudo/8701495 to your computer and use it in GitHub Desktop.
Read data line by line and turn in to JSON
var fs = require('fs');
var readline = require('readline');
var stream = require('stream');
var instream = fs.createReadStream('test.txt', 'utf8');
var outstream = new stream;
var rl = readline.createInterface(instream, outstream);
var obj = {}
rl.on('line', function (line) {
var lines = line.split(' ');
obj[lines[0]] = lines[1];
});
rl.on('close', function() {
console.log(JSON.stringify(obj));
});
{
"1": "中文",
"2": "or",
"3": "&&",
"4": "abc",
"5": "CD"
}
1 中文
2 or
3 &&
4 abc
5 CD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment