310

professional dilettante

CSV fun with Ruby

Working on a project and coming to dislike Excel due to my being an inept user of it!  I recall in Windows XP, Excel 2003, one could sort and filter a spreadsheet by blanks.  I was doing some manual work and was hoping to use the “Filter by blank” to save me some time at [...]

Working on a project and coming to dislike Excel due to my being an inept user of it!  I recall in Windows XP, Excel 2003, one could sort and filter a spreadsheet by blanks.  I was doing some manual work and was hoping to use the “Filter by blank” to save me some time at the end and delete a row if one column were empty.  This did not work out; Excel showed me the empty rows but they were not grouped together in way that let me delete them easily.   So I wrote a little ruby thing to:

-read the first csv
-create a new csv
-add only rows where both columns contain a string, to the new csv

Obviously this is a very specific problem and I’m sure I was doing something wrong in Excel. Although I came across a lot of premium Excel Add-ons that promised to extend Excel’s “Complex Filter” capability. But it was useful and fun to brush up on some basic Ruby. This script can probably serve as a skeleton for anyone looking to parse one CSV and create a second CSV with set criteria for which data is included. I just used the built in CSV classes and modules, which I hear are replaced in Ruby 1.9 by FasterCSV. But I’m not running 1.9 yet so yeah.

Here’s some test csv data

url,dest
/blah.com/whatever,/blah.com/destination
,2nd element first was empty
filled, also filled
,first was empty

require 'rubygems'
require 'csv'
data = CSV.parse(File.open(ARGV[0]).read.gsub("\r", "\n").strip)
#data.inspect
outfile = File.open(ARGV[1], "wb")
CSV::Writer.generate(outfile) do |csv|
<span class="indent-1">data.each do |row|
<span class="indent-2">target = row[0]
#redir = row[1]
#target_redir = #{ target } "," #{ redir }
unless target.nil?
<span class="indent-3" style="display:inline">csv &lt;&lt; row</span>
end</span>
end</span>
outfile.close
end

Maybe tonight I will try to replicate in Python and PHP :)

Leave a Reply

Required fields are marked with an asterisk (*), you may use these tags in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free

Most Recent Post

It’s been a long time, I shouldn’t have left you, part 1 …

But now I’m back.All official like.I do social stuff again.I see my friends.I’m moving into my own place again.And all that adult shit.

Categories

Content © professional dilettante
Proudly powered by WordPress
Theme designed by Artisan Themes

Entries (RSS)
Comments (RSS)

22 queries.
0.518 seconds.