# CSV with fastercsv

**URL:** <https://www.ruby-forum.com/t/csv-with-fastercsv/193605>\
**Category:** Rails\
**Created:** [August 20, 2010, 12:50pm UTC](https://www.ruby-forum.com/t/csv-with-fastercsv/193605 "2010-08-20T12:50:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ciri22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@ciri22](https://www.ruby-forum.com/u/ciri22)\
**Post date:** [August 20, 2010, 12:50pm UTC](https://www.ruby-forum.com/t/csv-with-fastercsv/193605/1 "2010-08-20T12:50:35Z")

</div>

I am creating a csv file with fastercsv.

require ‘fastercsv’  
FasterCSV.open(“file.csv”, “w”) do |csv|  
csv \<\< [45678, 45678, “stringa”]  
end

I would also print the last cell in quotation marks. Is there anyone who  
can help me?

---

<div class="post-metadata">

**Author:** ![ciri22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@ciri22](https://www.ruby-forum.com/u/ciri22)\
**Post date:** [August 20, 2010, 2:14pm UTC](https://www.ruby-forum.com/t/csv-with-fastercsv/193605/2 "2010-08-20T14:14:11Z")

</div>

Fil S. wrote:

> I would also print the last cell in quotation marks.

I also want to print the quotes in the last cell.

---

<div class="post-metadata">

**Author:** ![ciri22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@ciri22](https://www.ruby-forum.com/u/ciri22)\
**Post date:** [August 20, 2010, 2:54pm UTC](https://www.ruby-forum.com/t/csv-with-fastercsv/193605/3 "2010-08-20T14:54:25Z")

</div>

This isn’t a Rails question.

But, can’t you just put the double quotes insides single quotes?

csv \<\< [45678, 45678, ‘“stringa”’]

---

<div class="post-metadata">

**Author:** ![ciri22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@ciri22](https://www.ruby-forum.com/u/ciri22)\
**Post date:** [August 20, 2010, 3:07pm UTC](https://www.ruby-forum.com/t/csv-with-fastercsv/193605/4 "2010-08-20T15:07:18Z")

</div>

On Aug 20, 2010, at 6:50 AM, Filippo Salvatici wrote:

> I am creating a csv file with fastercsv.
> 
> require ‘fastercsv’  
> FasterCSV.open(“file.csv”, “w”) do |csv|  
> csv \<\< [45678, 45678, “stringa”]  
> end
> 
> I would also print the last cell in quotation marks. Is there anyone  
> who  
> can help me?

If you look at the spec for CSV, the proper contents of file.csv are:

45678,45678,stringa

You might put quotes around the string, but they are not needed for  
the file to be valid. However, if you had a comma in the string  
csv \<\< [1,2,“a,b”]  
then the file will contain  
1,2,“a,b”  
so that the comma isn’t seen as a value separator.

Be aware that FasterCSV (which is just CSV in Ruby1.9) is quite strict  
about adherence to the standard and many files that CSV will complain  
are malformed will be quietly parsed by MS-Excel. (Yes, I know you  
must be shocked about that 😉

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![ciri22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@ciri22](https://www.ruby-forum.com/u/ciri22)\
**Post date:** [August 20, 2010, 3:51pm UTC](https://www.ruby-forum.com/t/csv-with-fastercsv/193605/5 "2010-08-20T15:51:57Z")

</div>

if I write:  
csv \<\< [45678, 45678, ‘“stringa”’]

the result is:

45678,45678,""“stringa”""

☹

But I would like this:

45678,45678,“stringa”

---

<div class="post-metadata">

**Author:** ![ciri22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@ciri22](https://www.ruby-forum.com/u/ciri22)\
**Post date:** [August 20, 2010, 3:02pm UTC](https://www.ruby-forum.com/t/csv-with-fastercsv/193605/6 "2010-08-20T15:02:46Z")

</div>

Tim S. wrote:

> This isn’t a Rails question.
> 
> But, can’t you just put the double quotes insides single quotes?
> 
> csv \<\< [45678, 45678, ‘“stringa”’]

Seconded, this is a Ruby question.

Single quotes will work perfectly. You could also use %Q

csv \<\< [45678, 45678, %Q{stringa}]

---

<div class="post-metadata">

**Author:** ![ciri22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@ciri22](https://www.ruby-forum.com/u/ciri22)\
**Post date:** [August 20, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/csv-with-fastercsv/193605/7 "2010-08-20T18:39:15Z")

</div>

Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![ciri22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@ciri22](https://www.ruby-forum.com/u/ciri22)\
**Post date:** [August 20, 2010, 5:57pm UTC](https://www.ruby-forum.com/t/csv-with-fastercsv/193605/8 "2010-08-20T17:57:06Z")

</div>

On Aug 20, 2010, at 9:51 AM, Fil S. wrote:

> 45678,45678,“stringa”

You might “like” it, but the “quotes” are not needed in CSV unless the  
value requires them to be present (such as the presence of a comma or  
a quote.

Look at what you get with:

puts CSV.generate {|csv|  
csv \<\< [1,‘a’,‘“b”’, ‘c,c’, ‘d"d’]  
}

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![ciri22](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@ciri22](https://www.ruby-forum.com/u/ciri22)\
**Post date:** [August 20, 2010, 7:25pm UTC](https://www.ruby-forum.com/t/csv-with-fastercsv/193605/9 "2010-08-20T19:25:29Z")

</div>

> I am creating a csv file with fastercsv.
> 
> require ‘fastercsv’  
> FasterCSV.open(“file.csv”, “w”) do |csv|  
> csv \<\< [45678, 45678, “stringa”]  
> end
> 
> I would also print the last cell in quotation marks. Is there anyone who  
> can help me?

You may want :force\_quotes…

:force\_quotes: When set to a true value, FasterCSV will quote all CSV  
fields it creates.

FasterCSV.open(“file.csv”, “w”, :force\_quotes =\> true)…

That may also quote the numbers, I don’t recall…

-philip
