如何使用包含管道分隔值的JavaScript读取.psv文件内容。
文件内容为:
A|1|"A india "|1|"A hyderabad "
A|1|"A india "|2|"A banglore "
A|1|"A india "|3|"A chennai "
A|1|"A india "|4|"A amaravathi "
A|1|"A india "|5|"A delhi "
A|1|"A india "|6|"A uttarpradesh "
A|1|"A india "|7|"A west bengal "
A|1|"A india "|8|"A punjab "
A|1|"A india "|9|"A mizoram "
A|1|"A india "|10|"A rajasthan "
A|1|"A india "|11|"A kerala "
A|1|"A india "|12|"A mumbai "
B|1|"B USA "|1|"B florida "
B|1|"B USA "|2|"B new york "
B|1|"B USA "|3|"B texas "
B|1|"B USA "|4|"B miami "
B|1|"B USA "|5|"B alanta "
B|1|"B USA "|6|"B chicago "
B|1|"B USA "|7|"B washington "
B|1|"B USA "|8|"B boston "
B|1|"B USA "|9|"B los angels "
这是我尝试过的代码
var fs = require('fs');
fs.readFile('sample-file.psv', (err, data) => {
if (err) throw err;
console.log(data);
});
预期输出:
[{ a : india },{ b : USA },{ india : hyderabad },{ india : banglore },{ india : chennai },{ india : kerala },{ USA : newyork },{ USA : boston },{ USA : florida }]
有人知道怎么做吗? 请帮助解决此问题,并在此先感谢
答案 0 :(得分:1)
您可以通过以下方式获得分割内容的数据:
data = `A|1|"A india "|1|"A hyderabad "
A|1|"A india "|2|"A banglore "
A|1|"A india "|3|"A chennai "
A|1|"A india "|4|"A amaravathi "
A|1|"A india "|5|"A delhi "
A|1|"A india "|6|"A uttarpradesh "
A|1|"A india "|7|"A west bengal "
A|1|"A india "|8|"A punjab "
A|1|"A india "|9|"A mizoram "
A|1|"A india "|10|"A rajasthan "
A|1|"A india "|11|"A kerala "
A|1|"A india "|12|"A mumbai "
B|1|"B USA "|1|"B florida "
B|1|"B USA "|2|"B new york "
B|1|"B USA "|3|"B texas "
B|1|"B USA "|4|"B miami "
B|1|"B USA "|5|"B alanta "
B|1|"B USA "|6|"B chicago "
B|1|"B USA "|7|"B washington "
B|1|"B USA "|8|"B boston "
B|1|"B USA "|9|"B los angels "`
data.split('\n').map(function(line){
console.log(line.split('|'))
})