Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>NOTE:</strong> I concocted this solution before I was reminded about all the "special cases" that can occur in a valid CSV file, like escaped quotes. I'm leaving my answer for those who want something quick and dirty, but I recommend <a href="https://stackoverflow.com/a/12289296/901048">Evan's answer</a> for accuracy.</p> <hr> <p>This code will work when your <code>data.txt</code> file is one long string of comma-separated entries, with no newlines:</p> <p>data.txt:</p> <pre><code> heading1,heading2,heading3,heading4,heading5,value1_1,...,value5_2 </code></pre> <p>javascript:</p> <pre><code>$(document).ready(function() { $.ajax({ type: "GET", url: "data.txt", dataType: "text", success: function(data) {processData(data);} }); }); function processData(allText) { var record_num = 5; // or however many elements there are in each row var allTextLines = allText.split(/\r\n|\n/); var entries = allTextLines[0].split(','); var lines = []; var headings = entries.splice(0,record_num); while (entries.length&gt;0) { var tarr = []; for (var j=0; j&lt;record_num; j++) { tarr.push(headings[j]+":"+entries.shift()); } lines.push(tarr); } // alert(lines); } </code></pre> <p>The following code will work on a "true" CSV file with linebreaks between each set of records:</p> <p>data.txt:</p> <pre><code>heading1,heading2,heading3,heading4,heading5 value1_1,value2_1,value3_1,value4_1,value5_1 value1_2,value2_2,value3_2,value4_2,value5_2 </code></pre> <p>javascript:</p> <pre><code>$(document).ready(function() { $.ajax({ type: "GET", url: "data.txt", dataType: "text", success: function(data) {processData(data);} }); }); function processData(allText) { var allTextLines = allText.split(/\r\n|\n/); var headers = allTextLines[0].split(','); var lines = []; for (var i=1; i&lt;allTextLines.length; i++) { var data = allTextLines[i].split(','); if (data.length == headers.length) { var tarr = []; for (var j=0; j&lt;headers.length; j++) { tarr.push(headers[j]+":"+data[j]); } lines.push(tarr); } } // alert(lines); } </code></pre> <p><a href="http://jsfiddle.net/mblase75/dcqxr/" rel="noreferrer">http://jsfiddle.net/mblase75/dcqxr/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload