使用 Node-ETL 的一个 space 或多个合适的定界符
Suitable Delimitor for one space or more using Node-ETL
使用 node-etl
,如果一个 space 分隔符:
,我可以将文件内容提取到 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"},
]
*/
现在,如果数据之间的 space 不固定(一个或多个)怎么办?什么是合适的分隔符?
data.txt
21 Ahmed UK
54 Jean Chine
65 Mourad Tunisia
找到了!
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"],
});
/*
使用 node-etl
,如果一个 space 分隔符:
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"},
]
*/
现在,如果数据之间的 space 不固定(一个或多个)怎么办?什么是合适的分隔符?
data.txt
21 Ahmed UK
54 Jean Chine
65 Mourad Tunisia
找到了!
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"],
});
/*