Insert casper javascript data to CSV file

2019-06-09 02:57发布

问题:

I want to pass data from casper JS to CSV file I am currently using

fs = require('fs');
fs.write('test.csv', "name, title", "a");
fs.write('test.csv', name.trim()+","+title.trim(), "a");

this is inside a loop so there are many records

but printing to the CSV file is not inserting to a new row. It currently writes like this and I only want 2 columns on this

Name    Title
name1   name1    name2   title2    name3   title3

回答1:

add linebreaks

fs.write('test.csv', name.trim()+","+title.trim()+"\n", "a");

CSVs vary, but all usually have four special chars,

  • delimiter (the comma)
  • escape char (usually \, use to escape the other special chars when used in the data
  • line delimiter (usually \n, the newline character),
  • wrapper, (usually quotes that wrap around each value)

So, if you really want a proper CSV you would do something like this.

function escapeCSVData(text){
    text = text.
        replace("\", "\\,"). // escape any escape chars
        replace(",", "\,"); // escape commas
    return '"'+text+'"'; // add quotes
}

fs.write('test.csv', escapeCSVData(name.trim())+","+escapeCSVData(title.trim())+"\n", "a");

I didn't test the above, but that should work for you.

EDIT

I just learned that commas are simply quoted, not escaped, so the function would actaully just look like this..

function escapeCSVData(text){
    return '"'+text+'"'; // add quotes
}


回答2:

Add a line break after each write

fs.write('test.csv', "name, title\n", "a");

edit

Please note that although this solves your new line (row) problem, CSVs are more than just adding commas between entries. If any of your data contains a comma, you will need to escape them.

https://en.wikipedia.org/wiki/Comma-separated_values

There are a few libraries for note, like https://github.com/wdavidw/node-csv#readme -- but if you know absolutely how your data is going to behave, you can go on with your solution.