How to append data to csv file
``a'' Write-only, starts at end of file if file exists, otherwise
creates a new file for writing.``a+'' Read-write, starts at end of
file if file exists, otherwise creates a new file for reading and
writing
use CSV.open("participants_info.csv", "a+")
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
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
How write into CSV file properly
csv << "\n"
Stackoverflow requires 30 characters in an answer, but I don't know what more to say.
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
Convert CSV column of strings to integers with CSV lib in Ruby 1.9
Aaron, just change the row and write it to your new file like this
require 'csv'
File.open('modified.csv', 'w') do |csv|
CSV.foreach('original.csv', :headers => true) do |row|
row['Estimated Weekend Gross'] = row['Estimated Weekend Gross'].delete(',').to_i
row['Cume'] = row['Cume'].delete(',').to_i
csv << row
end
end
EDIT: if you want to save the headers in modified.csv you can do it like this, but there must be a shorter way without opening the file twice, if someone has a better solution for this ?
headers = CSV.open('original.csv', 'r', :headers => true).read.headers
CSV.open('modified.csv', 'w') do |csv|
csv << headers
CSV.foreach('original.csv', :headers => true) do |row|
row['Estimated Weekend Gross'] = row['Estimated Weekend Gross'].delete(',').to_i
row['Cume'] = row['Cume'].delete(',').to_i
csv << row
end
end
Related Topics
Why Does Psych Yaml Interpreter Add Line Breaks Around 80 Characters
Ruby Imap "Changes" Since Last Check
Why Is a String Key for a Hash Frozen
Should Rbenv Be Installed System-Wide, or at a User Level
Simple_Form with Bootstrap Check Box
Breaking a Large Rails App into Smaller Apps
"Ruby.Exe Is Not Recognized as an Internal or External Command" in Windows 7
When to Use Self in Module's Methods
Get Time from Datetime Variable in Ruby
How to Return a Fixed Length Binary Representation of an Integer in Ruby
Error Connecting to Redis on 127.0.0.1:6379 (Errno::Econnrefused) - Wercker
Ruby on Rails:How to Implement Cancel Button in Form_Tag
Sending Http Post Request in Ruby by Net::Http
Devise Not Working Well with Multiple Subdomains on Ror3 Application
Sorting an Array of Arrays in Ruby