2013-05-31 98 views
1

所以我解析一個大的csv文件並將結果推送到mongo中。從nodejs中包含重音字符的文件中讀取

該文件是maxminds city database。它有各種有趣的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 
}); 

什麼可能我做錯了嗎? 我得到這樣的事情在蒙戈:Chteauguay,加拿大

編輯

我試着用不同的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」「VillaEspa a」 34.7667,-58.2000 ,,

+0

如果你只是讀'fs.createReadStream'文件與'UTF-8',是不是腐敗? csv模塊可以改變它嗎? – Joe

+0

很好的問題。我會看看 – mkoryak

回答

1

結果是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