# How Can I insert another column data into the CSV file when I use FasterCSV?

**URL:** <https://www.ruby-forum.com/t/how-can-i-insert-another-column-data-into-the-csv-file-when-i-use-fastercsv/170199>\
**Category:** Rails\
**Created:** [June 13, 2009, 6:08pm UTC](https://www.ruby-forum.com/t/how-can-i-insert-another-column-data-into-the-csv-file-when-i-use-fastercsv/170199 "2009-06-13T18:08:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chenpassion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@chenpassion](https://www.ruby-forum.com/u/chenpassion)\
**Post date:** [June 13, 2009, 6:08pm UTC](https://www.ruby-forum.com/t/how-can-i-insert-another-column-data-into-the-csv-file-when-i-use-fastercsv/170199/1 "2009-06-13T18:08:21Z")

</div>

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 C…

---

<div class="post-metadata">

**Author:** ![chenpassion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@chenpassion](https://www.ruby-forum.com/u/chenpassion)\
**Post date:** [June 14, 2009, 4:16am UTC](https://www.ruby-forum.com/t/how-can-i-insert-another-column-data-into-the-csv-file-when-i-use-fastercsv/170199/2 "2009-06-14T04:16:04Z")

</div>

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 C…

2009/6/14 MaurÃ­cio Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chenpassion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@chenpassion](https://www.ruby-forum.com/u/chenpassion)\
**Post date:** [June 14, 2009, 5:41am UTC](https://www.ruby-forum.com/t/how-can-i-insert-another-column-data-into-the-csv-file-when-i-use-fastercsv/170199/3 "2009-06-14T05:41:56Z")

</div>

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://codeshooter.wordpress.com/) | [http://twitter.com/mauriciojr](http://twitter.com/mauriciojr)

---

<div class="post-metadata">

**Author:** ![chenpassion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@chenpassion](https://www.ruby-forum.com/u/chenpassion)\
**Post date:** [June 13, 2009, 11:52pm UTC](https://www.ruby-forum.com/t/how-can-i-insert-another-column-data-into-the-csv-file-when-i-use-fastercsv/170199/4 "2009-06-13T23:52:45Z")

</div>

Have you looked at the FasterCSV docs?

[http://fastercsv.rubyforge.org/classes/FasterCSV.html](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://codeshooter.wordpress.com/) | [http://twitter.com/mauriciojr](http://twitter.com/mauriciojr)
