Quote all fields in CSV output

前端 未结 2 365
北荒
北荒 2021-01-17 10:24
@out = File.open(\"#{File.expand_path(\"CSV\")}/#{file_name}.csv\", \"w\")
CSV::Writer.generate(@out) do |csv|
  csv << [\"01\", \"02\", \"test\"]
end
@out.clo         


        
相关标签:
2条回答
  • 2021-01-17 10:58

    Change

    CSV::Writer.generate(@out)do |csv|
    

    to

    CSV::Writer.generate(@out, {:force_quotes=>true}) do |csv|
    
    0 讨论(0)
  • 2021-01-17 11:07

    So why not double quote them?

    @out = File.open("#{File.expand_path("CSV")}/#{file_name}.csv", "w")
    CSV::Writer.generate(@out) do |csv|
      csv << ['"01"', '"02"', '"test"']
    end
    @out.close
    
    0 讨论(0)
提交回复
热议问题