解析Rails CSV到哈希

时间:2015-03-27 14:53:16

标签: ruby-on-rails ruby csv

我正在尝试解析rails中的.csv文件但事情却很奇怪。当我将其转换为哈希时,我无法访问某些键,并且值并不总是与正确的标题正确关联。

每个row.to_hash看起来像这样:

{"\"Name\""=>"\"Windows Explorer\"", "\"Start\""=>"\"15/03/26 5:34:00 PM\"", "\"End\""=>"\"15/03/26 5:34:10 PM\"", "\"Duration\""=>"\"0:00:10\"", "\"Process\""=>"\"Windows Explorer\""} 

row["\"Process\""]返回"\"Windows Explorer\""

但是,row["\"Name\""]会返回nil

我不确定为什么它不会返回"\"Windows Explorer\""

任何想法?


def self.import(file)

    mappings = {"\"Start\"" => "Start", "\"End\"" => "End","\"Duration\"" => "Duration","\"Process\"" => "Process","\"Name\"" => "Name"}

    CSV.foreach(file.path, headers: true, :quote_char => "\'") do |row| #need to escape quotes for import
        csv_row = row.to_hash
        csv_row = Hash[csv_row.map {|k, v| [mappings[k] || k, v] }] # remove escaped characters
        TimeLog.create(start_time: csv_row["Start"], end_time: csv_row["End"], duration: csv_row["Duration"], application: csv_row["Process"], document: csv_row["Name"])
    end

end

csv示例

"Name","Start","End","Duration","Process"
"Windows Explorer","15/03/26 5:34:00 PM","15/03/26 5:34:10 PM","0:00:10","Windows Explorer"
"Info","15/03/26 5:33:53 PM","15/03/26 5:34:00 PM","0:00:07","Time Doctor Lite"
"Question","15/03/26 5:33:47 PM","15/03/26 5:33:53 PM","0:00:06","Time Doctor Lite"
"AutoCAD","15/03/26 5:33:33 PM","15/03/26 5:33:47 PM","0:00:14","AutoCAD"
"ManicTime - TRIAL","15/03/26 5:33:28 PM","15/03/26 5:33:33 PM","0:00:05","ManicTime"
"Downloads","15/03/26 5:33:22 PM","15/03/26 5:33:28 PM","0:00:06","Windows Explorer"
"My Drive - Google Drive - Google Chrome","15/03/26 5:32:33 PM","15/03/26 5:33:22 PM","0:00:49","Google Chrome"
"Downloads","15/03/26 5:32:19 PM","15/03/26 5:32:33 PM","0:00:14","Windows Explorer"
"Microsoft Excel - Ryan App Tracking Thu.xlsx","15/03/26 5:32:14 PM","15/03/26 5:32:19 PM","0:00:05","Microsoft Excel"
"Save As","15/03/26 5:31:53 PM","15/03/26 5:32:14 PM","0:00:21","Microsoft Excel"
"Microsoft Excel - Ryan App Tracking Tue.xlsx","15/03/26 5:31:37 PM","15/03/26 5:31:53 PM","0:00:16","Microsoft Excel"
"ManicTime - TRIAL","15/03/26 5:31:21 PM","15/03/26 5:31:37 PM","0:00:16","ManicTime"
"My Drive - Google Drive - Google Chrome","15/03/26 5:31:10 PM","15/03/26 5:31:21 PM","0:00:11","Google Chrome"
"Inbox - ryan@coincraft.co - CoinCraft Mail - Google Chrome","15/03/26 5:31:01 PM","15/03/26 5:31:10 PM","0:00:09","Google Chrome"
"Untitled - Google Chrome","15/03/26 5:30:56 PM","15/03/26 5:31:01 PM","0:00:05","Google Chrome"
"Form Submission - Habitech Enquiry - Get Started - admin@smartskin.com.au - Habitech Systems Mail - Google Chrome","15/03/26 5:30:47 PM","15/03/26 5:30:56 PM","0:00:09","Google Chrome"
"Inbox (263) - admin@smartskin.com.au - Habitech Systems Mail - Google Chrome","15/03/26 5:30:39 PM","15/03/26 5:30:47 PM","0:00:08","Google Chrome"
"Master","15/03/26 5:30:33 PM","15/03/26 5:30:39 PM","0:00:06","Windows Explorer"
"Overview - Report 26/03/2015 17:29 - Reports - Paymo 3 - Google Chrome","15/03/26 5:30:22 PM","15/03/26 5:30:33 PM","0:00:11","Google Chrome"

2 个答案:

答案 0 :(得分:2)

如果您只使用"作为quote_char,则不需要映射。

这是我使用您提供的数据在我的终端中运行时得到的结果:

2.1.2 :004 > CSV.foreach('data.csv', :headers => true, :quote_char => '"') { |r| puts r.to_hash }
{"Name"=>"Windows Explorer", "Start"=>"15/03/26 5:34:00 PM", "End"=>"15/03/26 5:34:10 PM", "Duration"=>"0:00:10", "Process"=>"Windows Explorer"}

答案 1 :(得分:1)

尝试row["\"Name\""]使用大写' N'喜欢它似乎在哈希。