Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @potatosalad potatosalad revised this gist Mar 29, 2011. 1 changed file with 24 additions and 14 deletions.
    38 changes: 24 additions & 14 deletions Ruby script to convert CSV to YAML
    Original 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
    # Remove ::Reader and it shall work in Ruby 1.9.x
    # 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'

    class String
    def unquote
    self.gsub(/^"|"$/, '')
    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

    # 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
    flag = ARGV.shift unless input.nil?
    flag ||= input || '--yaml'

    puts fixture; puts
    case flag
    when '--json' then
    puts records.to_json
    else
    puts records.to_yaml
    end
  2. Administrator renamed this gist Nov 8, 2010. 1 changed file with 32 additions and 31 deletions.
    Original 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
    #
    # "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
    #!/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
  3. @gavinlaking gavinlaking revised this gist Feb 25, 2009. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Textmate/Ruby script to convert CSV to YAML
    Original 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'

  4. @gavinlaking gavinlaking revised this gist Feb 25, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Textmate/Ruby script to convert CSV to YAML
    Original 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 = "#{row[1].downcase}_#{row[0]}:\n"
    fixture = "record_#{row[0]}:\n"
    fields.each_with_index do |field, i|
    fixture += " #{field}: \"#{row[i]}\"\n"
    end
  5. @gavinlaking gavinlaking created this gist Feb 25, 2009.
    29 changes: 29 additions & 0 deletions Textmate/Ruby script to convert CSV to YAML
    Original 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