Reputation: 258
I have a large csv file full of numbers separated by the '|' character, for example:
432452 | 543634
4122442 | 41256512
64523 | 12416
Then I read in the data as follows:
fs = require('fs')
fs.readFile('data/data.csv', 'utf8', function (err,data) {
if (err) {
return console.log(err);
console.log(data);
});
And everything outputs as it is in the file.
My issue is with using the JS split() and trim() methods to modify this output. I'm attempting to remove any excess whitespace with trim() and use the delimiter '|' to separate the numbers so that they each appear on a newline. For example after the split, it should look like:
432452
|
543634
Is this the correct way to read in and edit .CSV data? Whenever I use these methods I get very garbled results.
Upvotes: 0
Views: 151
Reputation: 758
You state you have a CSV file, except what you gave as an example is not a CSV file. Assuming your example is correct (i.e. you don't have a CSV), the following code should suffice:
str.replace(/ /g, '\n');
This assumes the format of your file is consistent. It simply replaces all individual spaces with a newline instead, and gives you the format you desire. For example:
var a = '234324 | 32424324\n234234 | 234243234';
console.log(a.replace(/ /g, '\n');
Results in:
234324
|
32424324
234234
|
234243234
being printed.
Upvotes: 1