所以我正在解析一个大的 csv 文件并将结果推送到 mongo 中。
该文件是 maxminds city database 。它有各种有趣的utf8字符。我仍然在某些城市名称中收到(?)符号。这是我读取文件的方式:
(使用csv Node 模块)
csv().from.stream(fs.createReadStream(path.join(__dirname, 'datafiles', 'cities.csv'), {
flags: 'r',
encoding: 'utf8'
})).on('record', function(row,index){
.. uninteresting code to add it to mongodb
});
我在这里可能做错了什么? 我在 mongo 中得到这样的东西:Châteauguay,加拿大
编辑:
我尝试使用不同的库来读取文件:
lazy(fs.createReadStream(path.join(__dirname, 'datafiles', 'cities.csv'), {
flags: 'r',
encoding: 'utf8',
autoClose: true
}))
.lines
.map(String)
.skip(1) // skips the two lines that are iptables header
.map(function (line) {
console.log(line);
});
它会产生同样糟糕的结果: 154252,"PA","03","卡佩兰尼亚","",8.3000,-80.5500,, 154220,"AR","01","西类牙别墅","",-34.7667,-58.2000,,
最佳答案
事实证明 maxmind 用 latin1 编码他们的东西。
这有效:
var iconv = require('iconv-lite')
lazy(fs.createReadStream(path.join(__dirname, 'datafiles', 'cities.csv')))
.lines
.map(function(byteArray) {
return iconv.decode(byteArray, 'latin1');
})
.skip(1) // skips the two lines that are iptables header
.map(function (line) {
//WORKS
关于node.js - 从nodejs中包含重音字符的文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16863905/