# FasterCSV

**URL:** https://rubytalk.org/t/fastercsv/46358
**Category:** ruby-talk
**Created:** [7 May 2008 21:55 UTC](https://rubytalk.org/t/fastercsv/46358 "2008-05-07T21:55:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Kris\_Windham](https://avatars.discourse-cdn.com/v4/letter/k/59ef9b/32.png) [@Kris\_Windham](https://rubytalk.org/u/Kris_Windham)
#### Post date: [7 May 2008 21:55 UTC](https://rubytalk.org/t/fastercsv/46358/1 "2008-05-07T21:55:59Z")

</div>

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](mailto:1,Dave,Wilson,dave@wilson.com)\n  
[2,James,Stevens,james@stevens.com](mailto: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](mailto:1,Dave,Wilson,dave@wilson.com),105 Street,Little Rock,AR\n  
[2,James,Stevens,james@stevens.com](mailto: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.

Here is what I have so far.. As you can see that is just the first half  
of the data.

.........................................................................

require "faster\_csv"

class CSVReadWrite

def readwrite  
&nbsp;&nbsp;tempfile = "csvs/temp.csv"  
&nbsp;&nbsp;&nbsp;if File.exists?("csvs/temp.csv")  
&nbsp;&nbsp;&nbsp;&nbsp;File.delete("csvs/temp.csv")  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;File.new("csvs/temp.csv", "w")

&nbsp;&nbsp;filenameusers = "csvs/Users.csv"  
&nbsp;&nbsp;filenameaddresses = "csvs/Addresses.csv"  
&nbsp;&nbsp;FasterCSV.foreach(filenameusers) do |line|

&nbsp;&nbsp;&nbsp;@results = line[0], line[1] , line[2]

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FasterCSV.open("csvs/tempout.csv", "a") do |csv|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;csv \<\< @results

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

end

r1 = CSVReadWrite.new()  
r1.readwrite

end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)
#### Post date: [7 May 2008 22:20 UTC](https://rubytalk.org/t/fastercsv/46358/2 "2008-05-07T22:20:33Z")

</div>

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)  
&nbsp;&nbsp;&nbsp;joined \<\< user.fields + address.fields  
end

[users, addresses, joined].map { |csv| csv.close }

\_\_END\_\_

Hope that helps.

James Edward Gray II

> **···**
>
> 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.

---

<div class="post-metadata">

### Author: ![Kris\_Windham](https://avatars.discourse-cdn.com/v4/letter/k/59ef9b/32.png) [@Kris\_Windham](https://rubytalk.org/u/Kris_Windham)
#### Post date: [7 May 2008 22:42 UTC](https://rubytalk.org/t/fastercsv/46358/3 "2008-05-07T22:42:46Z")

</div>

James Gray wrote:

> **···**
>
> > On May 7, 2008, at 4:55 PM, Kris Windham wrote:
> > 
> > > ,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)  
> > &nbsp;&nbsp;&nbsp;joined \<\< user.fields + address.fields  
> > end
> > 
> > [users, addresses, joined].map { |csv| csv.close }
> > 
> > \_\_END\_\_
> > 
> > Hope that helps.
> > 
> > James Edward Gray II
> 
> That helps immensely.
> 
> Thank you for your quick response...  
> I have just started using fastercsv as opposed to the built-in csv ...
> 
> I have a lot yet to do with this, but that gets me started.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
