James Gray
5/7/2008 10:21:00 PM
On May 7, 2008, at 4:55 PM, Kris Windham wrote:
> I am wanting to use faster_csv to read two csv files and
> append columns to the first from the second and write to a temp file.
>
> for example,
>
> 1st Csv
> ...............................................
> id,name_first,name_last,email\n
> 1,Dave,Wilson,dave@wilson.com\n
> 2,James,Stevens,james@stevens.com\n
>
> 2nd Csv
> .................................................
> address_street,address_city,address_state\n
> 105 Street,Little Rock,AR\n
> 205 Street,Dallas,TX\n
>
>
> Output
> ..............................................
> id
> ,name_first,name_last,email,address_street,address_city,address_state
> \n
> 1,Dave,Wilson,dave@wilson.com,105 Street,Little Rock,AR\n
> 2,James,Stevens,james@stevens.com,205 Street,Dallas,TX\n
>
> I was hoping some one could point in me in the right direction on
> how to
> get started with this.
What about something like:
require "rubygems"
require "faster_csv"
users = FCSV.open("csvs/Users.csv")
addresses = FCSV.open("csvs/Addresses.csv")
joined = FCSV.open("csvs/temp.csv", "w")
while (user = users.gets) and (address = addresses.gets)
joined << user.fields + address.fields
end
[users, addresses, joined].map { |csv| csv.close }
__END__
Hope that helps.
James Edward Gray II