Skip to content

Instantly share code, notes, and snippets.

@vaicine
Created May 24, 2013 15:37
Show Gist options
  • Save vaicine/5644393 to your computer and use it in GitHub Desktop.
Save vaicine/5644393 to your computer and use it in GitHub Desktop.
Dirty little csv to html table parser in ruby
#!/usr/bin/env ruby
require 'rubygems'
require 'csv'
require 'sinatra'
get '/' do
csv = CSV.read("test.csv")
html = "<table> \n"
row_count = csv.count
csv.each_with_index do |row, row_position|
data_count = row.count
row.each_with_index do |data, data_position |
if row_position == 0
html << "<thead><tr> \n" if data_position == 0
html << "<td>#{data}</td> \n"
html << "</tr></thead> \n" if data_position == data_count - 1
else
html << "<tbody> \n" if row_position == 1 && data_position == 0
html << "<tr> \n" if data_position == 0
html << "<td>#{data}</td> \n"
html << "</tr> \n" if data_position == data_count - 1
html << "</tbody> \n" if row_position == row_count - 1 && data_position == data_count - 1
end
end
end
html << "</table> \n"
html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment