Skip to content

Instantly share code, notes, and snippets.

@pablox-cl
Forked from reservationlive/Ruby script to convert CSV to YAML
Created November 7, 2010 17:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pablox-cl/666257 to your computer and use it in GitHub Desktop.
Save pablox-cl/666257 to your computer and use it in GitHub Desktop.
Simple Ruby CSV to YAML converter
#!/usr/bin/env ruby
#
# Originally written by http://redartisan.com/tags/csv
# Added and minor changes by Gavin Laking
#
# "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.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