0

I have a python code such as :

import csv
reader = csv.reader(open('myText.txt', 'r'), delimiter=",")
for row in reader:
print row[0] + 'is' + row[1] + '</br>'

I look for a similar action/code[1] in JS or JQuery. The name of this action is welcome too. I'am exploring JS and wondering if there is a way do get an online/offline csv, parse it, iterate, inject some HTML in my website accordingly.

[1]: more precisely I look for a JS translation of reader = csv.reader(open('myText.txt', 'r'), delimiter=",") , I can manage the rest.

Note: myText.txt will be this online file

Hugolpz
  • 17,296
  • 26
  • 100
  • 187
  • Parsing the text is simple enough in javascript, but opening the file may be an issue. You're aware that javascript runs client-side? Or will the file be accessible via a url? – Reinstate Monica Cellio Mar 11 '13 at 22:55
  • I'am aware JS run client side. I'am exploring JS and wondering if there is a way do get an online/offline csv, parse it, iterate, inject some HTML in my website accordingly. – Hugolpz Mar 11 '13 at 22:58
  • 1
    You can't open a local file with JS, due to obvious security reasons. If it's available via a url then you can do what you need (assuming it's on the same domain as the site). – Reinstate Monica Cellio Mar 11 '13 at 22:59
  • You mean "external file" ? Ok, so the target csv need to be on the same website. I can copy it on my website, yes. – Hugolpz Mar 11 '13 at 23:00

2 Answers2

2

For the given CSV file, I think something like this should suffice (which uses only jquery):

$.get('/path/to/pinyinipamapping.csv')
.done(function(csvData) {
    var body = $('body');
    csvData.split(/\r\n|\n/).forEach(function (rowStr) {
        if (rowStr[0] != '#') {
            var row = rowStr.split(',');
            body.append('<p>' + row[0] + 'is' + row[1] + '</p>');
        }
    });
});

However, this will not work for quoted commas, etc.

For a more complete CSV parsing look at Javascript code to parse CSV data which uses code from this blog post. Also, you can consider the jQuery-csv library, though it is in beta.

Community
  • 1
  • 1
musically_ut
  • 34,028
  • 8
  • 94
  • 106
  • I +1ed your answer but validated @Fabio's answer in 2 parts, 2 parts may be easier to adapt to various contexts. But I will use parts of your code too. Thanks & +1 ! – Hugolpz Mar 11 '13 at 23:14
  • 1
    Be careful, his answer does not ignore comments in CSV which start with '#'. – musically_ut Mar 11 '13 at 23:18
1

For a quick and simple file it could be something like this: (Code inspired by this answer)

 // Put here the url to the file
 url = "https://raw.github.com/cburgmer/cjklib/master/cjklib/data/pinyinipamapping.csv";

    $.ajax({
        type: "GET",
        url: url,
        dataType: "text",
        success: function(data) {processData(data);}
     });


function processData(allText) {
    // Get an array of lines
    var allTextLines = allText.split(/\r\n|\n/);
    // Get the number of columns using the first row
    var entries = allTextLines[0].split(',');
    var lines = [];

    // while there are elements in the row
    while (entries.length>0) {
        // remove that line, split it and store in our array 
        lines.push(entries.shift().split(','));
    }
    // Now do your stuff with the array lines

}
Community
  • 1
  • 1
Fábio Diniz
  • 10,077
  • 3
  • 38
  • 45
  • From @Archer's comments, I understand that this code will work ONLY if bot my script and csv are on the same domain, right ? – Hugolpz Mar 11 '13 at 23:04
  • 1
    I don't think it is that easy. [CORS Rules](https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS) are a little more complex. The hosting server would need to support it explicitly. – musically_ut Mar 11 '13 at 23:17
  • Also, it is more nitpicking, but you should mention in your answer that this is not complete [weapon grade CSV parsing](http://tools.ietf.org/html/rfc4180). For example, it will not ignore the comment lines (which start with '#') in the original csv file. – musically_ut Mar 11 '13 at 23:20