Wesley Chen
2009-Jun-13 16:07 UTC
How Can I insert another column data into the CSV file when I use FasterCSV?
Hi, All, Suppose I have a CSV file, there is data in it. * Column 1 Column2 Column 3 Column 4 Row1 a b c Row2 a2 b2 c2* You know, the column 4 is no data Now, I would like to insert data to Column 4, after save, the CSV file will be: * Column 1 Column2 Column 3 Column 4 Row1 a b c d Row2 a2 b2 c2 d2* How can I do that? Any suggestion would be quite appreciated. Thanks. Wesley Chen. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maurício Linhares
2009-Jun-13 21:51 UTC
Re: How Can I insert another column data into the CSV file when I use FasterCSV?
Have you looked at the FasterCSV docs? http://fastercsv.rubyforge.org/classes/FasterCSV.html Just parse the file and then write to it again with the new column. - Maurício Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Sat, Jun 13, 2009 at 1:07 PM, Wesley Chen<cjq.999-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, All, > Suppose I have a CSV file, there is data in it. > > Column 1 Column2 Column 3 Column 4 > Row1 a b c > Row2 a2 b2 c2 > > You know, the column 4 is no data > Now, I would like to insert data to Column 4, after save, the CSV file will > be: > > > Column 1 Column2 Column 3 Column 4 > Row1 a b > c d > Row2 a2 b2 > c2 d2 > > How can I do that? > > Any suggestion would be quite appreciated. > Thanks. > Wesley Chen. > > > >
Wesley Chen
2009-Jun-14 02:15 UTC
Re: How Can I insert another column data into the CSV file when I use FasterCSV?
When I use fcsv=FasterCSV.open("test.csv", "a+") fcsv << "Hello" I can only add "Hello" to the end row of the csv. But how can I insert another column to the CSV? Thanks. Wesley Chen. 2009/6/14 Maurício Linhares <mauricio.linhares-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > Have you looked at the FasterCSV docs? > > http://fastercsv.rubyforge.org/classes/FasterCSV.html > > Just parse the file and then write to it again with the new column. > > - > Maurício Linhares > http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr > > > > On Sat, Jun 13, 2009 at 1:07 PM, Wesley Chen<cjq.999-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, All, > > Suppose I have a CSV file, there is data in it. > > > > Column 1 Column2 Column 3 Column > 4 > > Row1 a b c > > Row2 a2 b2 c2 > > > > You know, the column 4 is no data > > Now, I would like to insert data to Column 4, after save, the CSV file > will > > be: > > > > > > Column 1 Column2 Column 3 Column > 4 > > Row1 a b > > c d > > Row2 a2 b2 > > c2 d2 > > > > How can I do that? > > > > Any suggestion would be quite appreciated. > > Thanks. > > Wesley Chen. > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maurício Linhares
2009-Jun-14 03:41 UTC
Re: How Can I insert another column data into the CSV file when I use FasterCSV?
I said open the file, parse it again and re-generate it with the new column: FasterCSV.open("path/to/file.csv", "w") do |csv| FasterCSV.foreach("path/to/new/file.csv") do |row| csv << (row.fields + [ new_column_value ]) end end - Maurício Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Sat, Jun 13, 2009 at 11:15 PM, Wesley Chen<cjq.999-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> When I use > fcsv=FasterCSV.open("test.csv", "a+") > fcsv << "Hello" > > I can only add "Hello" to the end row of the csv. > > But how can I insert another column to the CSV? > > Thanks. > Wesley Chen. > > > 2009/6/14 Maurício Linhares <mauricio.linhares-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> >> Have you looked at the FasterCSV docs? >> >> http://fastercsv.rubyforge.org/classes/FasterCSV.html >> >> Just parse the file and then write to it again with the new column. >> >> - >> Maurício Linhares >> http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr >> >> >> >> On Sat, Jun 13, 2009 at 1:07 PM, Wesley Chen<cjq.999-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > Hi, All, >> > Suppose I have a CSV file, there is data in it. >> > >> > Column 1 Column2 Column 3 Column >> > 4 >> > Row1 a b c >> > Row2 a2 b2 c2 >> > >> > You know, the column 4 is no data >> > Now, I would like to insert data to Column 4, after save, the CSV file >> > will >> > be: >> > >> > >> > Column 1 Column2 Column 3 Column >> > 4 >> > Row1 a b >> > c d >> > Row2 a2 b2 >> > c2 d2 >> > >> > How can I do that? >> > >> > Any suggestion would be quite appreciated. >> > Thanks. >> > Wesley Chen. >> > >> > > >> > >> >> > > > > >