Insert casper javascript data to CSV file

亡梦爱人 提交于 2019-12-08 13:50:07

问题


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.



来源:https://stackoverflow.com/questions/35928005/insert-casper-javascript-data-to-csv-file

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