【问题标题】:Fast-csv writing all in a single line. Need multiple line outputFast-csv 将所有内容写在一行中。需要多线输出
【发布时间】:2019-03-12 11:50:54
【问题描述】:

我有一个 API 调用循环到 JSON 并通过 fast-csv 输出。但是,它将全部输出到 csv 文件中的一行中。有没有办法让它写成多行?

axios.get(url+'term='+orgName +'&location='+city + state + zipCode,{
            headers: {
                Authorization: 'Bearer ' + token
            }
        })
        /*If results are less than 1, moves on to fetchWhitePages API, ELSE, passes in the data*/
            .then(res => {
                if(Array.isArray(res.data.businesses) && res.data.businesses.length <= 0){
                   return fetchWhitePages(data);
                }else{
                    console.log('RUNNING YELPAPI');
                    /*For loop to get JSON objects within YelpAPI */

                    for(let i =0; i < res.data.businesses.length; i++ ){

                        churchName.push(res.data.businesses[i].name + '\n\r');
                        churchAddress.push(res.data.businesses[i].location.display_address + '\n\r');
                        churchPhone.push(res.data.businesses[i].phone + '\n\r' );

                        writeStream()




const churchName = [];
const churchAddress = [];
const churchPhone = [];

function writeStream (){
    fast
        .writeToPath("my.csv", [
                {name: churchName, address: churchAddress, phone: churchPhone}
        ], {
            headers: true,
            transform: function(row){
                return {
                    Name: row.name,
                    Address: row.address,
                    Phone: row.phone
                };
            }
        })
        .on("finish", function(){
            console.log("done!");
        });
}

【问题讨论】:

  • 不确定它是否与回答相关,但您介意发布整个 .then 块吗?
  • 我添加了我正在使用的写流方法。
  • @Christoph 看到添加。全部打印在一行中。我想要做的是三列名称,地址,电话。并相应地在其下方打印每一行。
  • .then 块仍然不完整,代码示例看起来很丑,请修复。我现在没有时间输入编写良好的解决方案,但您的代码中实际上存在两个问题。 1)fast-csv.writeToPath 可能会在每次调用时覆盖文件,但无法从文档中确认,2)您需要传递一组教堂对象,每个对象都有名称、地址和电话。您正在传递一个具有三个属性的对象,每个属性都包含一个大数组。所以这个单个对象被写入一行。

标签: javascript node.js csv export-to-csv


【解决方案1】:

您的代码中需要修复两件事:

  1. 只在 for 循环之后调用 writeToPath 一次。
  2. 构造一组教堂对象,每个对象恰好代表一个教堂。

这就是它的样子:

axios.get(url + 'term=' + orgName + '&location=' + city + state + zipCode, {
        headers: {
            Authorization: 'Bearer ' + token
        }
    })
    /*If results are less than 1, moves on to fetchWhitePages API, ELSE, passes in the data*/
    .then(res => {
        if (Array.isArray(res.data.businesses) && res.data.businesses.length <= 0) {
           return fetchWhitePages(data);
        } else {
            console.log('RUNNING YELPAPI');
            /*For loop to get JSON objects within YelpAPI */

            const churches = [];
            for(let i =0; i < res.data.businesses.length; i++ ){

                churches.push({
                    name: res.data.businesses[i].name + '\n\r';                 
                    address: res.data.businesses[i].location.display_address + '\n\r';
                    phone: res.data.businesses[i].phone + '\n\r';
                });

            }

            writeFile(churches);
       }
  });


function writeStream (churches) {
    fast.writeToPath("my.csv", churches, {
        headers: true,
        transform: function(row){
            // you could eliminate this block by changing the property names in the church objects
            return {
                Name: row.name,
                Address: row.address,
                Phone: row.phone
            };
        }
    })
    .on("finish", function(){
        console.log("done!");
    });
}

如果这解决了您的问题,请不要忘记将其标记为答案。

【讨论】:

  • 谢谢,“const Churches = {}”应该是那个,而不是像你这样的[]?还是我错过了什么?
  • 不,应该是[]。这是一个空数组,您可以将所有这些临时教堂对象附加到其中。 {} 将是一个常规对象,将对象附加到一个对象没有多大意义(即使使用 Javascript 它实际上甚至可以工作)。
猜你喜欢
  • 1970-01-01
  • 2015-03-12
  • 1970-01-01
  • 2014-04-16
  • 2021-10-24
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 2018-09-20
相关资源
最近更新 更多