Skip to content

Instantly share code, notes, and snippets.

@taylortrimble
Created January 23, 2013 21:54
Show Gist options
  • Save taylortrimble/4614189 to your computer and use it in GitHub Desktop.
Save taylortrimble/4614189 to your computer and use it in GitHub Desktop.
I needed to convert a nasty text document with formatted emails to CSV, so I made this.
require 'csv'
class User
attr_accessor :email, :first_name, :last_name
def initialize(email = nil, first_name = nil, last_name = nil)
@email = email
@first_name = first_name
@last_name = last_name
end
def initialize_with_formatted_email email
email.strip!
parts = email.split
if parts.count == 3
@email = parts[2].gsub(/[<>]/, "")
@first_name = parts[0]
@last_name = parts[1]
else
@email = parts[0]
end
end
end
emails = File::open("/Users/Trimble/Desktop/emails.txt", "r").readlines
CSV.open("/Users/Trimble/Desktop/emails.csv", "wb") do |csv|
emails.each do |email|
email.strip!
email.chomp! ","
user = User.new
user.initialize_with_formatted_email email
csv << [user.email, user.first_name, user.last_name]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment