javascript - How to get an html table out of a plain text table -


i have data stored in plain text files. uses spaces seperate different columns in different tables columns have different width (different number of characters). content of table data includes words, integers, floats , ranges.

is simple way extract data in javascript , transpile html table? prefer kind of general approach can used tables (means had detect position of new column - fixed indexes impossible because mentioned differ file file).

here example how 1 of these plain text tables like:

line1    23     45.4     12 - 14 line2    4      5.9      < 8 line3    13.56  105.34   20.37 - 130.20 line4    7.2    14.2     10.1 - 14.0 ... 

i think guy here has right solution you.

he's splitting lines on basis of spaces.

so once data in array, can loop through arrays , append string html tags form html table. can refer displayhtml() method in peeto's answer.

let me know need further it.

edit **

so, per sample data provided, i'm assuming 2 or more spaces change new column. if way can try code below.

var data = `23     45.4     12 - 14 4      5.9      < 8 13.56  105.34   20.37 - 130.20 7.2    14.2     10.1 - 14.0`;  data = data.split(/\r?\n/); // split text lines  var lines = []; (var = 0; < data.length; i++) {   data[i] = data[i].trim();   lines.push(data[i].split(/[ ][ ]+/)); // split lines further based on 2 or more spaces } // creating html string var htmlstr = '<table id=\'mytable\'>'; (var = 0; < lines.length; i++) {   htmlstr += '<tr>';   (var j = 0; j < lines[i].length; j++) {     htmlstr += '<td>' + lines[i][j] + '</td>';   }   htmlstr += '</tr>'; } htmlstr += '</table>';  document.getelementbyid('mydiv').innerhtml = htmlstr; // append string wherever 

i hope helps you. if still doesn't, need have close on files , find @ least 1 similarity between them pattern being followed change column.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -