Write CSV in Ruby 1.9 and CSV::Writer

write csv in ruby 1.9 and CSV::Writer

The csv library is still there, but CSV::Writer is not. According to the csv.rb in 1.9.0:

# I'm sure I'll miss something, but I'll try to mention most of the major
# differences I am aware of, to help others quickly get up to speed:
#
# === CSV Parsing
#
# * This parser is m17n aware. See CSV for full details.
# * This library has a stricter parser and will throw MalformedCSVErrors on
# problematic data.
# * This library has a less liberal idea of a line ending than CSV. What you
# set as the <tt>:row_sep</tt> is law. It can auto-detect your line endings
# though.
# * The old library returned empty lines as <tt>[nil]</tt>. This library calls
# them <tt>[]</tt>.
# * This library has a much faster parser.
#
# === Interface
#
# * CSV now uses Hash-style parameters to set options.
# * CSV no longer has generate_row() or parse_row().
# * The old CSV's Reader and Writer classes have been dropped.
# * CSV::open() is now more like Ruby's open().
# * CSV objects now support most standard IO methods.
# * CSV now has a new() method used to wrap objects like String and IO for
# reading and writing.
# * CSV::generate() is different from the old method.
# * CSV no longer supports partial reads. It works line-by-line.
# * CSV no longer allows the instance methods to override the separators for
# performance reasons. They must be set in the constructor.

A little later on, there's an example of how to write line-by-line (as well as other methods of writing):

# === To a File
#
# CSV.open("path/to/file.csv", "wb") do |csv|
# csv << ["row", "of", "CSV", "data"]
# csv << ["another", "row"]
# # ...
# end

How write into CSV file properly


csv << "\n"

Stackoverflow requires 30 characters in an answer, but I don't know what more to say.

How to create CSV file using CSV gem in ruby 1.9.2?

First of all, you need to swap loops if you are trying to put all the user data in the same file, and not overwrite it for every user:

CSV.open(filename, 'w') do |csv|
users.each do |u|
products = Product.find(:all,:conditions=>["user_id=?",u.id])

Next, fix your Excel (I suspect the output is taken from it, right?) to use comma as a separator, not a "space or comma".

Come back with the file contents attached and an example of CSV file which works for you if it still doesn't work.

Output array to CSV in Ruby

To a file:

require 'csv'
CSV.open("myfile.csv", "w") do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
# ...
end

To a string:

require 'csv'
csv_string = CSV.generate do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
# ...
end

Here's the current documentation on CSV: http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html

Split output data using CSV in Ruby 1.9

The short answer is "no". You'll want to adjust your current code to split up the set and then dump each subset to a different file. This ought to be pretty close:

export_rows.each_slice(1000).with_index do |rows, idx|
CSV.open("#{PATH_TO_EXPORT_FILE}/newexport-#{idx.to_s}.csv", "w+", :col_sep => '|', :headers => true) do |f|
rows.each { |row| f << row }
end
end

In Ruby CSV, how to write a blank ,, instead of ,, to a file?

Use

data = ["column one",nil,"column three"]

which generates that CSV

first,second,third
column one,,column three

Wice_Grid CSV export not working - Uninitialized constant CSV::Writer

Somewhere, that you haven't posted, you are referencing CSV::Writer. This works locally because you're using Ruby 1.8.7, but your production server is using Ruby 1.9.1. CSV::Writer was deprecated with Ruby 1.9.

From the the docs:

# * The old CSV's Reader and Writer classes have been dropped.

Step one is to upgrade your local Ruby to the same version as the server. This will give you the same error locally, which should go away once you find and remove that CSV::Writer.

The CSV docs give examples of how to use the current CSV class to accomplish what CSV::Writer used to do. Here's an example:

# == Writing
#
# === To a File
#
# CSV.open("path/to/file.csv", "wb") do |csv|
# csv << ["row", "of", "CSV", "data"]
# csv << ["another", "row"]
# # ...
# end

Upgrading Ruby will probably raise other errors. But Ruby 1.8.7 was retired in 2013 so these are problems you're going to want to fix now rather than later.

Good luck!

I am writing into a csv file but when i open it nothing is there

You are opening the CSV with the wrong flags.

"r" flag opens the file in read-only mode.

When you want to write to a file, you need to open it using the w (write) mode, or a (write append).

CSV.open("GreenTea.csv", "ab") do |csv|
csv << [current.ObjectID, current.PropertyID, current.PropertyValue, current.SourceID]
end

Ruby allows the following open modes:

  • "r" Read-only, starts at beginning of file (default mode).
  • "r+" Read-write, starts at beginning of file.
  • "w" Write-only, truncates existing file
    to zero length or creates a new file for writing.
  • "w+" Read-write, truncates existing file to zero length
    or creates a new file for reading and writing.
  • "a" Write-only, each write call appends data at end of file.
    Creates a new file for writing if file does not exist.
  • "a+" Read-write, each write call appends data at end of file.
    Creates a new file for reading and writing if file does
    not exist.


Related Topics



Leave a reply



Submit