Forked from reservationlive/Ruby script to convert CSV to YAML
Created
July 24, 2013 17:11
-
-
Save rcorreia/6072492 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Originally written by http://redartisan.com/tags/csv | |
# Added and minor changes by Gavin Laking | |
# Remove ::Reader and it shall work in Ruby 1.9.x | |
# | |
# "id","name","mime_type","extensions","icon_url" | |
# "1","unknown","unknown/unknown","||","/images/icon/file_unknown.gif" | |
# "2","image/tiff","image/tiff","|tiff|tif|","/images/icon/blank.png" | |
# | |
# if you want to remove the id: "number" line from the resulting YAML file | |
# do a find and replace for: ^( id: \"\d*\"\n) in Textmate | |
require 'csv' | |
class String | |
def unquote | |
self.gsub(/^"|"$/, '') | |
end | |
end | |
# first line contains the field names | |
line = gets | |
fields = line.split('","').collect {|f| f.unquote.chomp} | |
CSV::Reader.parse(STDIN) do |row| | |
fixture = "record_#{row[0]}:\n" | |
fields.each_with_index do |field, i| | |
fixture += " #{field}: \"#{row[i]}\"\n" | |
end | |
puts fixture; puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment