How can write a column in tsv file using node js?

廉价感情. 提交于 2019-12-08 10:08:28

问题


I have to write a json response in a column of tsv file how can I do that? I am using the following code. Please find me a solution? I have to check it but its not working.

//npm init -y  gen package.json file
var unProcessedItems = [];
var data = loadData('./Alabama_Pre_Final.tsv');

async function X(i) {
  if (data[i] && data[i][7]) {
    console.log(data[i][7]);

    function address(address_details) {
      request({
          url: 'https://us-extract.api.smartystreets.com/?auth-id=xxx&auth-token=xxx',
          method: 'POST',
          headers: {
              'content-type': 'application/json'
          },
          body: address_details,
      },
          (error, response, body) => {
              if (!error && response.statusCode == 200) {
                  var res = JSON.parse(body);
                  let objectArray = [];
                  if (res.addresses[0].verified) {
                      objectArray.push(res.addresses[0].api_output[0].delivery_line_1, res.addresses[0].api_output[0].last_line, res.addresses[0].api_output[0].delivery_point_barcode)
                  }
                  var address_data = objectArray.join([separator = ','])
                  console.log(address_data)
              }
          });
  }

  address("1000 Greenhill Blvd NW, Fort Payne, 35967, AL, USA");
  const data = 'Column 19\n1\t2\t3\n4\t5\t6';
  require('fs').writeFileSync('./Alabama_Pre_Final.tsv', data);

    //  i++;
    //  X(i);
  }
}
}

回答1:


I've looked at your answer above I think it should be easy enough to modify so that it works as you expect, I've done this below. The other functions should be left unchanged. So the function X processes each row then returns a record variable which we append to the records array. After processing is complete we write the records array to the csv file.

async function loadData(filePath) {
    if (fs.existsSync(filePath)) {
        var tsvData = fs.readFileSync(filePath, 'utf-8');
        var rowCount = 0;
        var scenarios = [];
        parse_tsv(tsvData, (row) => {
            rowCount++;
            if (rowCount > 1) {
                scenarios.push(row);
            }
        });
        return scenarios;
    } else {
        console.log("loadData: Returning empty..")
        return [];
    }
}

// Process a row of TSV data
function X(row) {
    return new Promise((resolve, reject) => {
        if (row && row[7]) {
            console.log(row[7]);
            request({
                url: 'https://us-extract.api.smartystreets.com/?auth-id=e62698e8-c3fc-b929-0f5b-d3b54d0bcd0c&auth-token=cibfMexBdl3HrmwbWY6p',
                method: 'POST',
                headers: { 'content-type': 'application/json' },
                body: row[7],
            },
                (error, response, body) => {
                if (!error && response.statusCode == 200) {
                    var res = JSON.parse(body);
                    let objectArray = [];
                    if (res.addresses[0].verified) {
                    objectArray.push(res.addresses[0].api_output[0].delivery_line_1, res.addresses[0].api_output[0].components.city_name,
                        res.addresses[0].api_output[0].components.zipcode, res.addresses[0].api_output[0].components.state_abbreviation)
                    }
                    var address_data = objectArray.join([separator = ','])
                    resolve({ name: address_data.replace(/['"]+/g, '') });
                } else if (error) { 
                    reject(error);
                } else {
                    reject( { statusCode: response.statusCode });
                }
            });
        }
    });
}

async function processData() {
    let MAX_RECORDS = 5; // Change as appropriate
    var data = await loadData('./Alabama_Pre_Final.tsv');
    if (data.length > 0) {
        unProcessedItems = [];
        let records = [];
        for(let row of data) {
            // Process a row of data.
            let record = await X(row);
            records.push(record);
            if (records.length === MAX_RECORDS) break;
        }
        console.log(records);
        csvWriter.writeRecords(records)
            .then(() => console.log('The CSV file was written successfully'));
    } else {
        console.log("No Data");
    }
}

processData();



回答2:


const fs = require('fs');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
var rp = require('request-promise');
var dataToWrite;
var http = require("http");
var request = require('request');

function loadData(filePath) {
if (fs.existsSync(filePath)) {
var tsvData = fs.readFileSync(filePath, 'utf-8');
var rowCount = 0;
var scenarios = [];
parse_tsv(tsvData, (row) => {
rowCount++;
if (rowCount > 1) {
scenarios.push(row);
}
});
return scenarios;
} else {
return [];
}
}

function parse_tsv(s, f) {
var ix_end = 0;
for (var ix = 0; ix < s.length; ix = ix_end + 1) {
ix_end = s.indexOf('\n', ix);
if (ix_end == -1) {
ix_end = s.length;
}
//var row = s.substring(ix, ix_end - 1).split('\t');
var row = s.substring(ix, ix_end).split('\t');
f(row);
}
}

var unProcessedItems = [];
var data = loadData('./Alabama_Pre_Final.tsv');
var records = [];
async function X(i) {
    if (data[i] && data[i][7]) {
        console.log(data[i][7]);

      var options = {
        method: 'POST',
        url: 'https://us-extract.api.smartystreets.com/?auth-id=c64de073-9531-9444-35e6-7204d9d62c36&auth-token=e5FxYc1niUD7Cp0peixd',
        headers: {
          'content-type': 'application/json'
        },
        body: data[i][7],
        json: true // Automatically stringifies the body to JSON
      };
      rp(options)
        .then(function (parsedBody) {
          // POST succeeded...
          var res = parsedBody;
          let objectArray = [];
          if (res.addresses.length) {
            if (res.addresses[0].verified) {
              objectArray.push(res.addresses[0].api_output[0].delivery_line_1, res.addresses[0].api_output[0].components.city_name,
                res.addresses[0].api_output[0].components.zipcode, res.addresses[0].api_output[0].components.state_abbreviation)
            }
            var address_data = objectArray.join([separator = ','])
            console.log(address_data)
            // records.push({ name: address_data.replace(/['"]+/g, '') });
            const ADDRESS_COLUMN_INDEX = 7;
            for(let row of address_data) {
                row[ADDRESS_COLUMN_INDEX] = await X(row[ADDRESS_COLUMN_INDEX]);
            }
            records.push(address_data.replace(/['"]+/g, ''));
            let output = records.map(row => row.join("\t")).join("\n");           
            i++;
            if (i <= 7) {
              X(i);
            } 
            else {
             fs.writeFileSync('out.tsv', output);
              console.log('The TSV file was written successfully');
              // return callback(records);
            }
          }
        });
    }
    // console.log(records);
  }
  if (data.length > 0) {
    unProcessedItems = [];
    X(0);
  } else {
    console.log("No Data");
  }



回答3:


I've updated this answer based on your last revisions, I hope this helps you:

function loadData(filePath) {
    if (fs.existsSync(filePath)) {
        var tsvData = fs.readFileSync(filePath, 'utf-8');
        var rowCount = 0;
        var scenarios = [];
        parse_tsv(tsvData, (row) => {
            rowCount++;
            if (rowCount > 1) {
                scenarios.push(row);
            }
        });
        return scenarios;
    } else {
        return [];
    }
}

function parse_tsv(s, f) {
    var ix_end = 0;
    for (var ix = 0; ix < s.length; ix = ix_end + 1) {
        ix_end = s.indexOf('\n', ix);
        if (ix_end == -1) {
            ix_end = s.length;
        }
        var row = s.substring(ix, ix_end).split('\t');
        f(row);
    }
}

var unProcessedItems = [];
var data = loadData('./Alabama_Pre_Final.tsv');
var records = [];
async function X(i) {
    if (data[i] && data[i][7]) {
        console.log(data[i][7]);

    var options = {
        method: 'POST',
        url: 'https://us-extract.api.smartystreets.com/?auth-id=c64de073-9531-9444-35e6-7204d9d62c36&auth-token=e5FxYc1niUD7Cp0peixd',
        headers: {
        'content-type': 'application/json'
        },
        body: data[i][7],
        json: true // Automatically stringifies the body to JSON
    };
    rp(options)
        .then(async function (parsedBody) {
            // POST succeeded...
            var res = parsedBody;
            let objectArray = [];
            if (res.addresses.length) {
                if (res.addresses[0].verified) {
                    objectArray.push(res.addresses[0].api_output[0].delivery_line_1, res.addresses[0].api_output[0].components.city_name,
                        res.addresses[0].api_output[0].components.zipcode, res.addresses[0].api_output[0].components.state_abbreviation)
                }
                var address_data = objectArray.join([separator = ','])
                console.log("rp.then -> address_data:",address_data);
                data[i][7] = address_data.replace(/['"]+/g, '');
                records.push(data[i].join("\t"));         
                i++;
                if (i <= 7) {
                    console.log("Looking up address #" + i);
                    X(i);
                } else {
                    const output = records.join("\n");  
                    // Remove the _test when you are happy with the result.
                    fs.writeFileSync('./Alabama_Pre_Final_test.tsv', output);
                    console.log('The TSV file was written successfully');
                }
            }
        });
    }
}
if (data.length > 0) {
    unProcessedItems = [];
    X(0);
} else {
    console.log("No Data");
}


来源:https://stackoverflow.com/questions/58371964/how-can-write-a-column-in-tsv-file-using-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!