Skip to content

Instantly share code, notes, and snippets.

@rjswenson
Created April 1, 2024 17:14
Show Gist options
  • Save rjswenson/6b494d94950c660c40c96a62a6cce570 to your computer and use it in GitHub Desktop.
Save rjswenson/6b494d94950c660c40c96a62a6cce570 to your computer and use it in GitHub Desktop.
cycle through a product csv and modify general color based on general or specific color name.
#!/usr/bin/env ruby
require 'csv'
# file = File.open('products_global_2.csv')
# file = File.rename(file, 'products_global_2-rename.csv')
# csv = CSV.open('products_global_2.csv', 'rb')
general_colors = %w[grey gray purple pink silver gold brown burlywood khaki navy beige indigo lime
red blue green orange yellow black white maroon tan turquoise sienna steel]
riders = %w[light dark]
def scan_and_grab(string, array)
array.each do |arr_str|
if /#{arr_str}/i =~ string
return arr_str.capitalize
end
end
return nil
end
rows_array = CSV.read('products_global.csv')
desired_indices = [8,9]
rows_array.map do |row|
current_gen = row[8].dup
current_color = row[9].dup
next if current_gen.nil? && current_color.nil?
if current_gen.nil? || current_gen.empty?
current_gen = current_color.dup
end
new_rider = scan_and_grab(current_gen, riders)
new_color = scan_and_grab(current_gen, general_colors)
if new_rider && new_color
row[8] = "#{new_rider} #{new_color}"
elsif new_color
row[8] = new_color
else
row[8] = ''
end
end
CSV.open('products_global-edit.csv', 'wb') {|csv| rows_array.each {|row| csv << row}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment