ruby - 没有这样的文件或目录@rb_sysopen

标签 ruby csv

当我运行这段代码时,我使用的是 Ruby 2.1.1:

<CSV.foreach("public/data/original/example_data.csv",headers: true, converters:              :numeric) do |info|

我得到一个错误:

No such file or directory @ rb_sysopen

如果我将 example_data.csv 放在同一个目录中,它会起作用,如下所示,但我的老板说不能这样他想要所有的 *.csv不同目录中的文件:

<CSV.foreach("example_data.csv",headers: true, converters: :numeric) do |info|

最佳答案

我不得不使用绕过文件实用程序的解决方法。使用 thoughtbot/paperclip 生成了一个名为 csvcontroller 的目录。我将 csv 文件放在该目录文件夹中。

class Uploader < ActiveRecord::Base
attr_accessible :purchase_name, :item_description, :item_price, :purchase_count,
                  :merchant_address, :merchant_name, :csvdata

has_attached_file :csvdata, :url => "/csvcontroller/:basename.:extension",

                :path => ":rails_root/csvcontroller/:basename.:extension"

                #:default_url => "/controllers/original/example_data.csv"

  validates_attachment_content_type :csvdata, :content_type => ["text/csv"]

end

然后我将我的解析器放在那个目录中以避免使用 FileUtils

require 'csv'

@total_cost = 0

#errors out FileUtils.move '/public/data/original/example_data.csv', '/controllers'

#errors out require File.expand_path('../app/public/data/original/', __FILE__)

# errors outCSV.foreach("any_path_name_outside_the_same_directory/example_data.csv", 
  #headers: true, converters: :numeric) do    |info|

CSV.foreach("example_data.csv", headers: true, converters: :numeric) do |info|

a =(info["item price"]).to_f

b = (info["purchase count"]).to_i

@total_cost += a * b
@store = []
customer = []
customer << info["purchaser name"]
@store << info["item description"]
@store << (info["item price"]).to_f
@store << (info["purchase count"]).to_i
@store << info["merchant address"]
@store << info["merchant name"]
puts @customer
puts @store
puts @total_cost
end

它看起来很丑,但事实就是如此。 我无法让 FileUtils::类正常工作。这是 2.1.1 的 Ruby 错误

关于ruby - 没有这样的文件或目录@rb_sysopen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22818611/

相关文章:

objective-c - obj-c 的 Ruby 解析器

ruby - 什么时候使用 %w?

arrays - 将元素从一个数组传递到另一个数组,生成 CSV

matlab - 如何将 .csv 文件读入 Matlab,其中每一列对应一个日期?

ruby - 了解 Ruby block 。为什么 Rspec 会这样做?

ruby - 导轨 : Is there a way of having only one contact form for a site that generates different HTML emails depending on what page it came from?

使用 mqueue.h 和 -lrt 配置 ceedling

windows - 比较两个 .CSV 文件并输出重复名称

mysql - 使用 LOAD DATA INFILE 从 CSV 文件导入 MySQL 中的数据

r - 我可以在 R 中并行读取 1 个大 CSV 文件吗?