Forum: Ruby Ruby CSV merge rows

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
36520e1b213525e749e12b9f472068e5?d=identicon&s=25 Arthur Petrov (arnthur1981)
on 2014-08-28 12:03
Hi! Every one! I just want to ask if any one knows how to merge rows in
Ruby CSV in this way:
Input
A  B  C  D  E  F <=headers
X  Y  1  1  0  0
X  Y  0  0  2  2


Output

A  B  C  D  E  F <=headers
X  Y  1  1  2  2

where 0 is empty cell.

Thank you very much for your help!!!!
4a65f01f7ece0b720bdb0de3c3db089e?d=identicon&s=25 Dansei Yuuki (blutorange)
on 2014-08-28 14:15
Use CSV#read.

> http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/C...

It will return either an array of array, or a CSV::Table object if you
added either :headers=>:first_row or :header=>true to the options hash.

Then use CSV::Table::by_col!

>
http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/C...

Then you can use [] and []= to access and write each column, iterate via
#each. This should get you started

> # csv is of class CSV::Table
> csv.each do |col|
>     # adapt to your needs
>     csv[col.first] = col.last.delete_if { |y| y == 'delete_me' }
> end

> http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/C...
4bede944460135d3f520e4b08bfd9348?d=identicon&s=25 Sourabh3110 Mashalkar (sourabh3110)
on 2016-09-20 10:43
How to merge 2 csv's into single(seperate) csv wrt ItemId(forein key)

'text1.csv' :

ItemId,MFD,AmmountInINR
FA1986,2015-03-07,742.0999999999999
FA1987,2015-03-07,6307.849999999999
FA1988,2015-03-08,445.26
FA1989,2015-03-08,890.52
FA1990,2015-03-09,7346.789999999999

'text2.csv':

ItemId,name,place
FA1986,sourabh,bengaluru
FA1987,swapneel,mumbai
FA1988,sunil,chennai
FA1989,suresh,delhi
FA1990,ramesh,kolkatta

merging should give me output in 'output.csv' :

ItemId,MFD,AmmountInINR,name,place
**details of these fields**
This topic is locked and can not be replied to.