FasterCSV

K

Kris Windham

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,[email protected]\n
2,James,Stevens,[email protected]\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,[email protected],105 Street,Little Rock,AR\n
2,James,Stevens,[email protected],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
 
J

James Gray

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,[email protected]\n
2,James,Stevens,[email protected]\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,[email protected],105 Street,Little Rock,AR\n
2,James,Stevens,[email protected],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
 
K

Kris Windham

James said:
,name_first,name_last,email,address_street,address_city,address_state
\n
1,Dave,Wilson,[email protected],105 Street,Little Rock,AR\n
2,James,Stevens,[email protected],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


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top