merging 2 csv files and sorting merged file

A

Askar Ka

Hello all!

Things I want to do:
1) merging 2 csv files
2) sorting new merged file
3) delete some columns from a new merged file

1st csv file1:

11101,3310000,IKANIKEISAIGANAIBAAI,NISHI-KU
SAITAMA-SHI,SAITAMA,0,0,0,0,0,0
11101,3310058,IIDA,NISHI-KU SAITAMA-SHI,SAITAMA,0,0,0,0,0,0
11101,3310068,IIDASHINDEN,NISHI-KU SAITAMA-SHI,SAITAMA,0,0,0,0,0,0

2nd csv file2:

12101,2600000,IKANIKEISAIGANAIBAAI,CHUO-KU CHIBA-SHI,CHIBA,0,0,0,0,0,0
12101,2600852,AOBACHO,CHUO-KU CHIBA-SHI,CHIBA,0,0,0,0,0,0
12101,2600804,AKAICHO,CHUO-KU CHIBA-SHI,CHIBA,0,0,0,0,0,0

The code below:
------------------------------------
require "fileutils"
FileUtils.cp("file1.csv", "file3.csv")

File.open("file3.csv", "a+")
File.open("file2.csv", "r") do |file|
file.each_line do |line|
puts line
end
end
------------------------------------

1st I made a copy of the 1st file, and then I try just to add the
content of the 2nd file. At the moment I can only print the content of
the 2nd file, but can't to add this the file3... :( This a problem 1.
2nd is sorting - please advise me directions which I need read more...

Thanks in advance!
 
A

Askar Ka

Here I post the answerI got from japanese mailing list,
which worked for me


out =3D File.open("file3.csv", "a+")
File.open("file2.csv", "r") do |file|
file.each_line do |line|
out.puts line # =E5=87=BA=E5=8A=9B
end
end

OR

File.open("file3.csv", "a+") do |out|
out.print IO.read('file2.csv')
end

-- =

Posted via http://www.ruby-forum.com/.=
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top