Skip to content

Instantly share code, notes, and snippets.

@hakusaro
Created November 3, 2015 08:15
Show Gist options
  • Save hakusaro/09cba423b4e1525d505f to your computer and use it in GitHub Desktop.
Save hakusaro/09cba423b4e1525d505f to your computer and use it in GitHub Desktop.
This parses CSVs for inserting into raw data blocks on Readme.io. Don't have commas in your values or it will break. Alternatively, change the root separator to something like a semicolon.
require 'json'
if ARGV[0] == nil
abort "You moron, you forgot to give us a CSV."
end
data = {}
file = File.new(ARGV[0])
start = true
cols = 0
data_rows = 0
file.each do |line|
if start
arr = line.split(",")
cols = arr.size
incr = 0
arr.each do |header|
data["h-#{incr}"] = header.chomp
incr += 1
end
start = false
next
end
incr = 0
line.split(",").each do |thingy|
data["#{data_rows}-#{incr}"] = thingy.chomp
incr += 1
end
data_rows += 1
end
result = {
data: data,
cols: cols,
rows: data_rows
}
puts JSON.pretty_generate(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment