Asked  9 Months ago    Answers:  5   Viewed   10 times

New to Node.js and trying to pull a value from the very last line of a CSV file. Here is the CSV:

Unit ID,Date,Time,Audio File
Log File Created,3/6/2013,11:18:25 AM,file:\C:UsersBenDocuments1_03-06-2013_1114-50.mp3
1,3/6/2013,11:20:24 AM,file:\C:AlertLog1_03-06-2013_1120-24.mp3
1,3/6/2013,11:20:39 AM,file:\C:AlertLog1_03-06-2013_1120-24.mp3

The part I am trying to grab is file:\C:AlertLog1_03-06-2013_1120-24.mp3 - preferably getting rid of the file:\ part.

Sorry that I do not have any code to show, just have a few hours of experience with Node.js and cannot seem to find any docs on how to accomplish something like this. Any help would be appreciated. Thanks!

 Answers

1

Regular file

Read the file like a regular file, split the file contents into lines, take the last line, split by a comma and take the last part.

var fs = require('fs'); // file system module

fs.readFile('/path/to/file.csv', 'utf-8', function(err, data) {
    if (err) throw err;

    var lines = data.trim().split('n');
    var lastLine = lines.slice(-1)[0];

    var fields = lastLine.split(',');
    var audioFile = fields.slice(-1)[0].replace('file:\\', '');

    console.log(audioFile);
});

File System module documentation

CSV parser

You can also use the node-csv-parser module.

var fs = require('fs');
var csv = require('csv');

csv()
 .from.stream(fs.createReadStream('/path/to/file.csv'))
 .to.array(function(data, count) {
    var lastLine = data.slice(-1)[0];
    var audioFile = lastLine.slice(-1)[0].replace('file:\\', '');
    console.log(audioFile);
  });
Wednesday, September 22, 2021
 
comte
 
1

Have a look at my answer to a similar question for C#. The code would be quite similar, although the encoding support is somewhat different in Java.

Basically it's not a terribly easy thing to do in general. As MSalter points out, UTF-8 does make it easy to spot r or n as the UTF-8 representation of those characters is just the same as ASCII, and those bytes won't occur in multi-byte character.

So basically, take a buffer of (say) 2K, and progressively read backwards (skip to 2K before you were before, read the next 2K) checking for a line termination. Then skip to exactly the right place in the stream, create an InputStreamReader on the top, and a BufferedReader on top of that. Then just call BufferedReader.readLine().

Friday, June 4, 2021
 
Yrtymd
 
1

The docs for node-csv-parser (npm install csv) specifically state that it can be used with streams (see fromStream, toStream). So it's not hard-coded to use stdout.

Several other CSV parsers also come up when you npm search csv -- you might want to look at them too.

Thursday, July 22, 2021
 
2

I did it something like this :

http.createServer(function(request, response) {
    response.setHeader('Content-disposition', 'attachment; filename=testing.csv');
    response.writeHead(200, {
        'Content-Type': 'text/csv'
    });

    csv().from(data).to(response)

})
.listen(3000);
Sunday, August 8, 2021
 
Peter
 
4

Building on Nouveau's code you could do this

for($i = 0; $i <= count($fieldList); $i++)
{
    if (is_numeric($fieldList[$i]))
    {
        if (strpos($fieldList[$i],'.') !== false){
            $fieldList[$i] = (int)$fieldList[$i];
        }else{
            $fieldList[$i] = (float)$fieldList[$i];
        }
    }

    switch(gettype($fieldList[$i])) {
        case 'integer':
            $typeInfo = 'int(11)';
            break;
        case 'float':
        case 'double':
            $typeInfo = 'float';
            break;

        case 'string':
            $typeInfo = 'varchar(80)';
            break;
        default:
            $typeInfo = 'varchar(80)';
            break;
    }
if(gettype($fieldList[$i]) != NULL) echo "t".'`'.$i.'` '.$typeInfo.' NOT NULL, --'.gettype($fieldList[$i]).' '.$fieldList[$i]."<br/>n";

}

That works, note the addition of "case 'double':" in the switch, but there may be a better way to do the int/float check as they would only work with standard uk/us numbers.

Friday, August 13, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :