在 ruby 1.9 和 CSV::Writer 中写入 csv [英] write csv in ruby 1.9 and CSV::Writer
问题描述
我有一个代码可以在 ruby 1.87 上正常工作,但不适用于 ruby 1.9.它说 CSV::Writer 未声明,但它仍然是 rdoc 的一部分.在fastercsv合并之后,csv api是否发生了变化?
i have a code that works fine with ruby 1.87 but dont works with ruby 1.9. It says that CSV::Writer is undeclared but it still part of the rdoc. Does the csv api changed, after the fastercsv merge, or not?
我的代码:
require 'csv'
def self.export_csv
file_name = File.join(RAILS_ROOT, 'public','csv',"#{start_date_f}_#{end_date_f}.csv")
return file_name if File.exist?(file_name)
@results = find(:all)
header_row = []
outfile = File.open(file_name, 'wb')
CSV::Writer.generate(outfile) do |csv|
header_row = ['gateway_id','created', 'gateway_status_id', 'panel_id', 'panel_status','volts_out', 'amps_out', 'temp','aid' ,'sid', 'pisid']
csv << header_row
end
end
我收到的错误:NameError: uninitialized constant CSV::Writer
The error that i receive: NameError: uninitialized constant CSV::Writer
请注意这里有 require 'csv'.我在控制台中尝试它,当我执行 require 'csv' 时,它可以工作,但是一旦我调用 CSV::Writer,我就会收到该错误.这段代码在 ruby 1.87 上运行良好,所以它让我认为这是一个 ruby 1.9 csv 问题,因为它与 fastCSV 合并.
Note that require 'csv' is there. i try it in my console, when i do the require 'csv', it works, but as soon as i call CSV::Writer i receive that error. This code works fine with ruby 1.87, so it makes me think that it is a ruby 1.9 csv problem since it was merged with fasterCSV.
推荐答案
csv 库仍然存在,但 CSV::Writer 不在.根据 1.9.0 中的 csv.rb:
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
这篇关于在 ruby 1.9 和 CSV::Writer 中写入 csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!