Code Monkey home page Code Monkey logo

Comments (9)

koles avatar koles commented on July 24, 2024

from ya-csv.

davidfufu avatar davidfufu commented on July 24, 2024

Hi, it seems it's just for console output. Writing to another file was fine. This is my code right now. Adding an index to the data seems to give me columns, but what if I want to output the data row by row?


var csv = require('ya-csv');
var fs = require('fs');
var streamout = fs.createWriteStream("output.txt");

// equivalent of csv.createCsvFileReader('data.csv')
var reader = csv.createCsvFileReader('chosen.csv', {
    'separator': ',',
    'quote': '"',
    'escape': '"',
    'comment': '',
});
var writer = new csv.CsvWriter(process.stdout);

reader.addListener('data', function(data) {
    console.log(data[1]);
    streamout.write(JSON.stringify(data[0]));
});

reader.addListener('error', function(e) {
    console.error('Oops!');
});

from ya-csv.

koles avatar koles commented on July 24, 2024

from ya-csv.

davidfufu avatar davidfufu commented on July 24, 2024

The console output has been resolved. Now, I'm just wondering how I can selectively output the data row by row in ya-csv using indexes.

var csv = require('ya-csv');
var fs = require('fs');
var streamout = fs.createWriteStream("output.txt");

var reader = csv.createCsvFileReader('myFile.csv', {
    'separator': ',',
    'quote': '"',
    'escape': '"',
    'comment': '',
});
var writer = new csv.CsvWriter(process.stdout);

reader.addListener('data', function(data) {
    console.log(data[0]);
    streamout.write(JSON.stringify(data[0]));
});

reader.addListener('error', function(e) {
    console.error('Oops!');
});

My code currently gives me a column of the csv table when I give an index to data such as "data[0]". I'm wondering if there's a ya-csv setting that will make it return rows instead of columns when I give it an index.

For example, if I did data[0], I'd want it to return the first row. What I see right now when I do that is just the first column like below.

Post ID

111519035531887_1898820693468370
111519035531887_1895819030435203
111519035531887_1895099177173855
111519035531887_1893797443970695
111519035531887_1888299321187174

Much Thanks.

from ya-csv.

koles avatar koles commented on July 24, 2024

Ah, I see. The ya-csv is designed to be stream processor with a minimal memory footprint.

Can you help me to understand a bigger picture, why do you prefer to access individual rows? Are you looking for a sort of a random access or is it more like about an ability to skip first N rows easily?

from ya-csv.

davidfufu avatar davidfufu commented on July 24, 2024

I'm doing some social media analytics and each row is essentially a post with a Post ID, and more data about engagement. It's just easier for me to work with and more logical. However, in the process of writing this, I tried.

for (var i =0; i < data.length; i++) {
 	console.log(data[i]);
 	}

This gives me each row item by item. Now this confuses me as "data[0]" gives the first column but a for loop with data[i] gives each item in each row.

I guess my question is answered, but I'm just confused now as to why indexing data can switch between rows and columns.

from ya-csv.

davidfufu avatar davidfufu commented on July 24, 2024

Is there any way for ya-csv to know when the order of the 'data' events? I'd like to have something trigger on the last 'data' event of the stream.

from ya-csv.

koles avatar koles commented on July 24, 2024

from ya-csv.

davidfufu avatar davidfufu commented on July 24, 2024

Yup, thanks for the help.

from ya-csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.