# FasterCSV

**URL:** <https://www.ruby-forum.com/t/fastercsv/136989>\
**Category:** Ruby\
**Created:** [May 7, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/fastercsv/136989 "2008-05-07T23:55:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![krisw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@krisw](https://www.ruby-forum.com/u/krisw)\
**Post date:** [May 7, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/fastercsv/136989/1 "2008-05-07T23:55:57Z")

</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,removed\_email\_address@domain.invalid\n  
2,James,Stevens,removed\_email\_address@domain.invalid\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,removed\_email\_address@domain.invalid,105 Street,Little Rock,AR\n  
2,James,Stevens,removed\_email\_address@domain.invalid,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  
tempfile = “csvs/temp.csv”  
if File.exists?(“csvs/temp.csv”)  
File.delete(“csvs/temp.csv”)  
end  
File.new(“csvs/temp.csv”, “w”)

filenameusers = “csvs/Users.csv”  
filenameaddresses = “csvs/Addresses.csv”  
FasterCSV.foreach(filenameusers) do |line|

@results = line[0], line[1] , line[2]

```
 FasterCSV.open("csvs/tempout.csv", "a") do |csv|
  csv << @results

   end

```

end

r1 = CSVReadWrite.new()  
r1.readwrite

end

---

<div class="post-metadata">

**Author:** ![krisw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@krisw](https://www.ruby-forum.com/u/krisw)\
**Post date:** [May 8, 2008, 12:22am UTC](https://www.ruby-forum.com/t/fastercsv/136989/2 "2008-05-08T00:22:32Z")

</div>

On May 7, 2008, at 4:55 PM, Kris W. wrote:

> ,name\_first,name\_last,email,address\_street,address\_city,address\_state  
> \n  
> 1,Dave,Wilson,removed\_email\_address@domain.invalid,105 Street,Little Rock,AR\n  
> 2,James,Stevens,removed\_email\_address@domain.invalid,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 G. II

---

<div class="post-metadata">

**Author:** ![krisw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@krisw](https://www.ruby-forum.com/u/krisw)\
**Post date:** [May 8, 2008, 12:42am UTC](https://www.ruby-forum.com/t/fastercsv/136989/3 "2008-05-08T00:42:46Z")

</div>

James G. wrote:

> On May 7, 2008, at 4:55 PM, Kris W. wrote:
> 
> > ,name\_first,name\_last,email,address\_street,address\_city,address\_state  
> > \n  
> > 1,Dave,Wilson,removed\_email\_address@domain.invalid,105 Street,Little Rock,AR\n  
> > 2,James,Stevens,removed\_email\_address@domain.invalid,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 G. 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.
