简体   繁体   中英

Ruby | CSV.generate options “quote_char” does not work

By this code.

require 'csv'
CSV.generate('', quote_char: '"') { |csv| csv << ['A','B','C'] }

It returns this.

A,B,C

But I expect this result.

"A","B","C"

Enviroment

  • Ruby 2.3.1

Use the force_quotes option:

puts CSV.generate(quote_char: '"', force_quotes: true) { |csv| csv << ['A','B','C'] }
"A","B","C"

Source: https://stackoverflow.com/a/5832131

First, your specification of quote_char is redundant, because this is the default.

quote_char does not say that quotes must be used, but indicates which quotes to use if something needs to be quoted. In your example, no quotes are needed, so you don't see any affect of the quote character.

If you want to have quotes always, you should set force_quotes: true .

See for instance here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM