我在ruby/rails中导入此CSV文件时遇到问题
我得到的错误信息是这样的:
Missing or stray quote in line 1 (CSV::MalformedCSVError)
Run Code Online (Sandbox Code Playgroud)
但我不确定发生了什么,因为我的CSV看起来非常好.以下是示例数据:
"lesley_grades","lesley_id","last","first","active","site","cohort","section","sections_title","faculty","completed_term_cred","term","sec_start_date","sec_end_date","grade","stc_cred","active_program","most_recent_program","intent_filed","stc_term_gpa","sta_cum_gpa","start_term","prog_status","last_change_date"
,1234456,John,Doe,TRUE,"Baltimore, MD",0002012,14/FA_ERLIT_6999_U15AA,Directed Independent Study,"Jane Hicks , Jill Saunders",2,14/FA,9/3/14,12/17/14,B-,2,EME.2270.TCBAL.01,EME.2270.TCBAL.01, ,3.3,3.148,12/SU,A,9/2/14
,1234455,John,Doe,TRUE,"Baltimore, MD",0002012,14/FA_ERSPD_6999_U15AG,Directed Independent Study,"Jane Hicks , Jill Saunders",3,14/FA,9/3/14,12/17/14,A-,3,EME.2270.TCBAL.01,EME.2270.TCBAL.01, ,3.3,3.148,12/SU,A,9/2/14
Run Code Online (Sandbox Code Playgroud)
为了给出上下文,有效的csv看起来像这样,lesley_grades作为第一列.假设所有迁移都是预先设置的,则over CSV脚本文件将查找第一列并检查是否激活了Active Record对象,然后将其存储为具有完全相同模型名称的db.
lesley_grades lesley_id last first active
1234556 Doe John TRUE
1123445 Doe John TRUE
Run Code Online (Sandbox Code Playgroud)
这是导致我出现问题的代码的一部分
def import!(csv)
csv_reader = CSV.parse(csv)
ActiveRecord::Base.transaction do
csv_reader.each do |row|
set_record_class_and_columns(row) if header_row?(row)
if columns_mapping_defined? && record_class_defined? && record_row?(row)
import_row(row)
end
end
if imports_failed?
puts 'Aborting importing and rolling back...'
show_errors
raise ActiveRecord::Rollback …Run Code Online (Sandbox Code Playgroud)