Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node.js 读取 csv iso-8859 乱码 #8

Open
sophister opened this issue Aug 2, 2018 · 1 comment
Open

node.js 读取 csv iso-8859 乱码 #8

sophister opened this issue Aug 2, 2018 · 1 comment
Labels

Comments

@sophister
Copy link
Owner

sophister commented Aug 2, 2018

csv文件编码如下:
image

下面这样,可以解决乱码问题,不能iso888591来解码,需要用 gbk来解码

let filePath = './test.csv';

const rawStr = fs.readFileSync(filePath);
let utf8 = iconv.decode(rawStr, 'gbk');
console.log(`after decode string:`, utf8);


parse(utf8, {}, (err, out) => {
    if( err ){
        return console.error(err);
    }
    console.log(`parse result: `, out);
});

暂时还没解决,先记录下过程中相关的文章:

@sophister
Copy link
Owner Author

sophister commented Aug 2, 2018

在测试中文乱码过程中,还发现 cvs-parse 这个库,貌似有点问题,也不可能是我用的姿势不对?

比如我的 cvs文件,只有 2行,像这样:
image

像下面这样,在 finish 事件里,获取解析出来的数据,少了最后一行

const fileStream = fs.createReadStream(filePath);
const rows = [];
const parser = parse({
    skip_empty_lines: true,
});
parser.on('readable', () => {
    //在 finish 事件触发后,还会再触发一次readable事件,这一次,能 read 到最后一行数据
    let record = null;
    while(record = parser.read()){
        rows.push(record);
    }
});
parser.on('error', (err) => {
    console.error('parser.error', err);
});
//这里应该用 end 事件
parser.on('finish', () => {
    // 这里在最后一行数据之前触发,导致 rows 里,缺少了最后一行数据
    console.log('finish rows: ', rows);
});

fileStream
    .pipe(iconv.decodeStream('gbk'))
    .pipe(parser);

给官方提了一个 issue ,看看是不是我的用法不对吧

确实是用法不对,应该用 end 事件,来最终获取结果。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant