Forked from potatosalad/Ruby script to convert CSV to YAML
Created
July 24, 2013 17:11
Revisions
-
potatosalad revised this gist
Mar 29, 2011 . 1 changed file with 24 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,9 @@ # # Originally written by http://redartisan.com/tags/csv # Added and minor changes by Gavin Laking # Rewritten by Andrew Bennett for Ruby 1.9 # # Usage: ruby csv_to_fixture.rb file.csv [--json] # # "id","name","mime_type","extensions","icon_url" # "1","unknown","unknown/unknown","||","/images/icon/file_unknown.gif" @@ -12,22 +14,30 @@ # do a find and replace for: ^( id: \"\d*\"\n) in Textmate require 'csv' require 'json' require 'yaml' input = ARGV.shift is_file = (input.nil? ? false : File.exist?(input)) file = is_file ? input : STDIN doc = is_file ? CSV.read(file) : CSV.parse(file.read) fields = doc.shift records = Hash.new doc.each_with_index do |row, i| record = Hash.new fields.each_with_index do |field, j| record[field] = row[j] end records["record_#{i}"] = record end flag = ARGV.shift unless input.nil? flag ||= input || '--yaml' case flag when '--json' then puts records.to_json else puts records.to_yaml end -
Administrator renamed this gist
Nov 8, 2010 . 1 changed file with 32 additions and 31 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,32 +1,33 @@ #!/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 -
gavinlaking revised this gist
Feb 25, 2009 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,6 +6,9 @@ # "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' -
gavinlaking revised this gist
Feb 25, 2009 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,7 @@ 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 -
gavinlaking created this gist
Feb 25, 2009 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ #!/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" 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 = "#{row[1].downcase}_#{row[0]}:\n" fields.each_with_index do |field, i| fixture += " #{field}: \"#{row[i]}\"\n" end puts fixture; puts end