从包含nodejs中的重音字符的文件读取

所以我parsing一个大的csv文件,并将结果推送到mongo。

该文件是maxminds城市数据库 。 它有各种有趣的utf8字符。 我仍然在一些城市名称中使用(?)符号。 这里是我如何阅读文件:

(使用csv节点模块)

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,加拿大得到这样的东西

编辑

我试着用不同的lib来读取文件:

 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”,“Capellan a”,“”,8.3000,-80.5500 ,, 154220,“AR”,“01”,“别墅Espa a”,“ ”,-34.7667,-58.2000 ,,

原来maxmind编码他们的东西拉丁文1。

这工作:

  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 
Interesting Posts