javascript - 使用 Node 将大数组写入文件

标签 javascript node.js json csv

我正在尝试将 csv 文件转换为数组中的对象并将其写入新文件。

我已使用 csv-parser 转换了数据,但将数组写入新文件并访问数组中的数据时遇到问题。

这是我转换 csv 文件的代码:

const csv = require('csv-parser');
const fs = require('fs');
const readStream = fs.createReadStream('agents.csv');
const writeStream = fs.createWriteStream('agent_data.txt');
let realtorArray = [];

readStream.pipe(csv())
.on('data', (data) => {
    realtorArray.push(data);
})

.on('end', () => {
    //I've tried writing the array to a file in three ways
    //The first way was JSON.stringify() but that failed due to the file size I think.
    //Second I tried toString-ing the array as below...

    writeStream.write(realtorArray.toString());

    //Third way was creating a buffer...

    let buf = Buffer.from(String(realtorArray));
    writeStream.write(buf);

    console.log('CSV file successfully processed');
    writeStream.end();
});

转换后的 CSV 数据如下所示:

[
    {
        'License Type': 'BRK',
        'License Number': '12345',
        'Full Name': 'John Doe',
        'License Status': '20',
        'Original License Date': '1234567',
        'License Expiration Date': '12345678',
        'Education Status': '0',
        'Agency Identifier': '5431424'
    }, {
        'License Type': 'BRK',
        'License Number': '4312241',
        'Full Name': 'Jane Doe',
        'License Status': '20',
        'Original License Date': '5433234',
        'License Expiration Date': '12345435',
        'Education Status': '0',
        'Agency Identifier': '11222234444'
    },
    ......
]

然后,当我以我尝试过的各种方式写入文件时......

当我使用.toString()时,数据如下所示:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]...............

当我创建缓冲区时,数据如下所示:

<Buffer 5b 6f 62 6a 65 63 74 20 4f 62 6a 65 63 74 5d 2c 5b 6f 62 6a 65 63 74 20 4f 62 6a 65 63 74 5d 2c 5b 6f 62 6a 65 63 74 20 4f 62 6a 65 63 74 5d 2c 5b 6f ... >

当我尝试读取数据时......

如果我在写入时缓冲或 toString() 数据并使用 JSON.stringify(data) 来读取它:

{
    "type": "Buffer",
    "data": [91,111,98,106,101,99,116,32,79,98,106,101,99,........]
}

如果我.toString()写入并尝试读取它,它看起来与上面相同或类似

[object Object],[object Object],[object Object],[object Object]......

有人知道我做错了什么吗?感谢您提前提供任何建议。

最佳答案

[object Object],[object Object],...[object Object]realtorArray.toString() 的正确结果另外,使用 String(realtorArray)会给你相同的结果。

JSON.stringify(realtorArray)将转换realtorArray转换成它的 JSON 等价物。所以...

writeStream.write(JSON.stringify(realtorArray));

应将您的数据写入文件。

关于javascript - 使用 Node 将大数组写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60031808/

相关文章:

node.js - 快速路由将变量传递到所需文件

android - 执行 http POST 返回 HTML 而不是 JSON

json - Postgres 9.3 中 JSON 和用户定义类型的比较

javascript - Javascript : Nested Return Statement, return inside Return

javascript - Discord JS 嵌入 react 导致另一个嵌入

javascript - 在使用 window.location 重定向后的弹出窗口中,window.close 变为未定义

android - 带有 Volley 的 Android Cardview 和 Recycler 错误

javascript - 正则表达式用 HTML 标签替换字符

javascript - 将字符串转换为整数时出现错误

MYSQL 8.0 - 客户端不支持服务器请求的身份验证协议(protocol)