Skip to content

Instantly share code, notes, and snippets.

@zoeabryant
Created April 10, 2018 18:04
Show Gist options
  • Save zoeabryant/e453f06608475392668f5825a81f81d3 to your computer and use it in GitHub Desktop.
Save zoeabryant/e453f06608475392668f5825a81f81d3 to your computer and use it in GitHub Desktop.
quick script to change a csv no problemo
require 'CSV'
puts "lets go ruby"
def fruit_we_want_to_change? fruit
fruits_affected = [
'apple',
'banana',
]
fruits_affected.include?(fruit)
end
CSV.open("output.csv", "wb") do |csv_out|
CSV.foreach("sample_data.csv") do |row|
fruit = row[0]
location = row[2]
if fruit_we_want_to_change?(fruit)
new_location = 'sainsburys'
puts 'changing ' + fruit + ' location to ' + new_location
row[2] = new_location
end
csv_out << row
end
end
# SAMPLE CSV:
# fruit,some irrelevant data,location
# apple,blah,england
# pineapple,blah,brazil
# banana,blah,india
# banana,blah,west indies
# banana,blah,antarctic
# mango,blah,pakistan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment