【发布时间】:2019-10-31 18:37:58
【问题描述】:
所以我正在努力让用户从 CSV 文件中导入数据。现在所有字段都将正确导入,除了第一个字段。
我发现文件类型正在影响导入。
我的代码如下:
class Import < Operation
require 'csv'
def call(file, training_event_id)
csv_data = CSV.parse(file.read, headers: true)
list_occo = []
csv_data.each do |row|
occupant = Occupant.new
occupant.account_number = row['Account Number']
occupant.check_in = row['Check In']
binding.pry
occupant.training_event_id = training_event_id
list_occo << occupant
end
binding.pry
occo_errors = check_file(list_occo)
list_occo.each(&:save) if occo_errors.empty?
return occo_errors
end
当我执行 binding.pry 并检查占用者时,我在执行 CSV UTF-8 时的帐号为零。如果我切换到直接 CSV 不是问题。有没有办法将 CSV UTF-8 转换/切换为 CSV?我想/尝试在解析上使用某种编码,例如: encoding: 'iso-8859-1' 但这不起作用。
有没有办法转换 CSV UTF-8,或者有没有办法直接检查文件格式以确保它是 CSV 而不是 CSV UTF-8?
【问题讨论】:
标签: ruby-on-rails