Created
April 10, 2018 18:04
-
-
Save zoeabryant/e453f06608475392668f5825a81f81d3 to your computer and use it in GitHub Desktop.
quick script to change a csv no problemo
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 characters
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