I only want to remove the extra commas but don’t want to shift any data. Just remove the extra commas and left that cell to be empty.
a | b | c | d | e | f |
---|---|---|---|---|---|
First row | 1 | 0 | , | , | , |
Second row | 1 | 1 | 3 | , | , |
Third row | 1 | , | 3 | , | , |
and I want =
a | b | c | d | e | f |
---|---|---|---|---|---|
First row | 1 | 0 | |||
Second row | 1 | 1 | 3 | ||
Third row | 1 | 3 |
I have written the following code, the extra commas get removed but the data get shifted to the previous column:
const fs = require('fs');
const readline = require('readline');
// Function to remove extra commas from a line
function removeExtraCommas(line) {
let withinQuotes = false;
let result = '';
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (char === '"') {
withinQuotes = !withinQuotes;
result += char;
} else if (char === ',' && withinQuotes) {
result += char;
} else if (char === ',' && !withinQuotes) {
const nextChar = line[i + 1];
if (nextChar === ',') {
result += '';
} else {
result += char;
}
} else {
result += char;
}
}
return result;
}
// Function to process the CSV file
function processCSVFile(filePath) {
const outputFilePath = '../70_primary_codesData2CSV/70 Primary Codes Data/19120/19120(828+828_new)withoutExtraS.csv';
const fileStream = fs.createReadStream(filePath);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
const outputStream = fs.createWriteStream(outputFilePath);
rl.on('line', (line) => {
const processedLine = removeExtraCommas(line);
outputStream.write(processedLine + 'n');
});
rl.on('close', () => {
console.log('CSV file processed successfully.');
});
}
// Example usage
const filePath = '../70_primary_codesData2CSV/70 Primary Codes Data/19120/19120(828+828_new).csv'; // Replace with your input file path
processCSVFile(filePath);
2
Answers
Try this:
you’ll be replacing by a space.
Issue with the
removeExtraCommas
function when it encounters an extra comma, is that it appends an emptystring
” to the result, hence the data shift to the previous column.The requirement should be to append an empty
value
to the result to ensure columns remain aligned.The solution would be to introduce a column variable to keep a track of the current column value.
See updated
removeExtraCommas
function below, this should resolve the data shift: