【发布时间】:2014-03-19 13:33:12
【问题描述】:
我有一个 CSV 文件可以通过两种方式解析:
上传文件并实时解析
上传文件并使用delayed_job解析它
当我实时解析文件时,没有问题,但是当我使用延迟作业解析文件时,我得到一个 CSV::Table 但带有 nil 字段 [nil,nil.....]
这是我在帮助程序中解析文件的方式:
CSV.parse(file.read, :headers => true, :row_sep =>:auto)
这是我保存 csv 文件并创建延迟作业的方式: 是的,它的 CSV,我打错了,但我仍然有问题,我以这样的操作读取文件:
def create
if params[:dump]
file = File.new(params[:dump][:file].original_filename, "w")
File.open(file.path, "w") { |f| f.write(params[:dump][:file].read.force_encoding('UTF-8')) }
@import = Import.create(:created_by => current_user.id, :import_file_path => file.path, :size => 0)
file.close
file_to_parse = File.open(@import.import_file_path)
if params[:dump][:file].size < 10000
parsed_file = parsed file_to_parse
if parsed_file && parsed_file.count > 0
@imported_contacts, @imported_organizations = extract_contacts_and_organizations_from parsed_file
validate_and_save @imported_organizations if @imported_organizations.any?
validate_and_save @imported_contacts if @imported_contacts.any?
@import.update_attributes(:done => true, :size => (@imported_contacts.size + @imported_organizations.size))
redirect_to org_import_path(current_org, @import)
else
redirect_to new_org_import_path(current_org) , :notice => "fichier vide ou introuvable ou ne pas de format cvs/vcf"
end
else
Delayed::Job.enqueue(ImportJob.new(current_org, current_user, @import.id))
redirect_to org_import_path(current_org, @import)
end
else
redirect_to new_org_import_path(current_org) , :notice => "vous devrez selctionner un fichier"
end
end
【问题讨论】:
-
我认为您混淆了 CVS 和 CSV。从您传递的论点来看,我认为您的意思是 CSV。 CVS 是一个源代码管理系统
-
更详细的代码示例会有所帮助/即您在文件中读取的位置、调用延迟作业的位置等。
-
是的,它的 CSV,我打错了,但我仍然有问题,我以这样的操作读取文件
-
我在问题中添加了如何上传文件以及如何延迟创建作业,感谢您的帮助,我真的找不到解决方案,非常感谢您的帮助
-
ImportJob 的代码在哪里?我们需要看看它是如何运行解析的,看看有什么区别。
标签: ruby-on-rails csv delayed-job