gpt4 book ai didi

ruby-on-rails - 将 Rails CSV 解析为哈希

转载 作者:太空宇宙 更新时间:2023-11-03 18:13:55 25 4
gpt4 key购买 nike

我正在尝试在 Rails 中解析 .csv 文件,但情况很奇怪。当我将它转换为散列时,我无法访问某些键,并且这些值并不总是与正确的 header 正确关联。

每个 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"

最佳答案

如果您只是使用 " 作为您的 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"}

关于ruby-on-rails - 将 Rails CSV 解析为哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29303607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com