我在阅读CSV文件时遇到一些麻烦。让我退后一步。 (我使用节点js,要求(' csv'))
所以我有两个CSV文件 -
us-area-code-cities.csv - 每行或实体包含美国区号和相应的纬度和经度值
201,Bayonne,New Jersey,US,40.66871,-74.11431
sampleData.csv - 包含来电的时间戳和来电者的电话号码
07-JUN-95 11.28.15,1234567890
我有两个单独的csv对象,每个CSV文件一个(我不确定这个,请告诉我这是不常见的):
var areaCodeCSV = csv();
var phoneDataCSV = csv();
首先,我需要填充字典,使用 us-area-code-cities.csv 将区号映射到纬度和经度对象。然后,当我浏览 sampleData.csv 中的每个csv实体时,我想查找调用者区号的纬度和经度值,并将区号转换为纬度/经度。但是,当我读取 sampleData.csv 文件并尝试查找时,似乎尚未填充字典。
代码:
areaCodeCSV.from.path('./public/us-area-code-cities.csv').to.array(function (data) {
for (var index = 0; index < data.length; index++) {
areaCodeDictionary[data[index][0]] = new geoLocation(data[index][4], data[index][5]);
}
console.log(areaCodeDictionary); //prints successfully
});
function convertAreaCodeToGeoObject(areaCode) {return areaCodeDictionary[areaCode]; }
phoneDataCSV.from.path('./public/sampleData.csv').to.array(function
(data) {
for (var index = 0; index < data.length; index++) {
var time = data[index][0];
var ac = convertAreaCodeToGeoObject(data[index][1].substring(0, 3)); //only want the first three digits
var year = parseInt("20" + time.substring(7, 9));
var month = convertMonthToNum(time.substring(3, 6));
var day = parseInt(time.substring(0, 2));
var hours = parseInt(time.substring(10, 12));
var minutes = parseInt(time.substring(13, 15));
var seconds = parseInt(time.substring(16));
console.log("ac: " + ac); //prints undefined
phoneData.push(new dataObject(new Date(year, month, day, hours, minutes, seconds, 0), ac));
console.log(phoneData); //prints {date : undefined,...,date : undefined}
是否必须同步读取这些CSV文件?我猜测一个是在另一个之前读取的,因此我想将区域代码转换为lat / long时,字典不会填充。这里有一个简单的解决方法吗?我想我能做的一件事就是对字典进行硬编码,但是如果我能够将其付诸实践那就太棒了。看起来很简单!我试图将一个放入一个函数并在另一个函数中调用该函数,但phoneData数组只是空的。
谢谢!
答案 0 :(得分:0)
您需要在回调函数中插入,即seconf文件的进程:
areaCodeCSV.from.path('./public/us-area-code-cities.csv').to.array(function (data) {
let areaCodeDictionary = [];
for (var index = 0; index < data.length; index++) {
areaCodeDictionary[data[index][0]] = new geoLocation(data[index][4], data[index][5]);
}
proccesSecondFile(areaCodeDictionary);
console.log(areaCodeDictionary); //prints successfully
});
function proccesSecondFile(areaCodeDictionary) {
function convertAreaCodeToGeoObject(areaCode) { return areaCodeDictionary[areaCode]; }
phoneDataCSV.from.path('./public/sampleData.csv').to.array(function(data) {
for (var index = 0; index < data.length; index++) {
var time = data[index][0];
var ac = convertAreaCodeToGeoObject(data[index][1].substring(0, 3)); //only want the first three digits
var year = parseInt("20" + time.substring(7, 9));
var month = convertMonthToNum(time.substring(3, 6));
var day = parseInt(time.substring(0, 2));
var hours = parseInt(time.substring(10, 12));
var minutes = parseInt(time.substring(13, 15));
var seconds = parseInt(time.substring(16));
console.log("ac: " + ac); //prints undefined
phoneData.push(new dataObject(new Date(year, month, day, hours, minutes, seconds, 0), ac));
}
console.log(phoneData);
//for websocket
//var asString = JSON.stringify(phoneData);
//console.log(asString);
});
}