使用node-etl
,如果有一个空格分隔符,我可以将文件内容提取到JS数组中:
data.txt
21 Ahmed UK
54 Jean Chine
65 Mourad Tunisia
load.js
var ETL=require('node-etl')
var people=ETL.extract('./data.txt',{
delimitor:" ", //just ONE Space . What about ONE or many spaces
headers:["id","name","country"],
});
/*
[
{id:"21",name:"Ahmed",country:"UK"},
{id:"54",name:"Jean",country:"Chine"},
{id:"65",name:"Mourad",country:"Tunisia"},
]
*/
data.txt
21 Ahmed UK
54 Jean Chine
65 Mourad Tunisia
答案 0 :(得分:0)
实测!
Node-ETL
包提供了一些现成的静态分隔符,对于这种情况,ETL.DLM_SPACE_OR_MORE
是合适的。
然后,代码应为:
var people=ETL.extract('./data.txt',{
delimitor:ETL.DLM_SPACE_OR_MORE, //just ONE Space . What about ONE or many spaces
headers:["id","name","country"],
});
/*