basic question...

M

mopthisandthat

Hello,

Suppose "OOO.csv" is a data file with 9 columns, and I want to create a
new file that is consisted of 2nd, 5th, and 7th columns of the OOO.csv
(i.e., subset of the original), how do I do this?

Thanks.
 
A

Alder Green

Hi

Iterate over OOO.csv's lines, split each line as you wish (probably
using slice notation is the easiest for fixed length columns), and
writing each slice into the desired destination file.

Regards,
Alder
 
J

James Edward Gray II

Hello,

Suppose "OOO.csv" is a data file with 9 columns, and I want to
create a
new file that is consisted of 2nd, 5th, and 7th columns of the OOO.csv
(i.e., subset of the original), how do I do this?

My suggestion, if you don't mind using a non-standard library:

Neo:~/Desktop$ ls
000.csv trim_columns.rb
Neo:~/Desktop$ cat 000.csv
1,2,3,4,5,6,7,8,9
a,b,c,d,e,f,g,h,i
Neo:~/Desktop$ ruby -rubygems trim_columns.rb 000.csv
2,5,7
b,e,g
Neo:~/Desktop$ cat trim_columns.rb
#!/usr/local/bin/ruby -w

require "faster_csv"

FasterCSV.filter { |row| row.replace(row.values_at(1, 4, 6)) }

__END__

Hope that helps.

James Edward Gray II
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top