Rails + 读取没有固定标题和键值对的XLSX或CSV文件

huangapple go评论49阅读模式
英文:

Rails + Read XLSX or CSV without fixed headers and key value pair

问题

在Rails中尝试读取没有固定标题的XLSX / CSV文件。

例如

第一个CSV文件,标题将类似于user_id、email、dob

第二个CSV文件,标题将类似于user_id、username、email

因此,系统应该能够正确读取它们,并应该创建正确的JSON,如下所示

对于第一个文件,JSON将如下所示

{
"user_id": 1,
"email": "example@eample.com",
"dob": "2001-01-01",
}

对于第二个文件,JSON将如下所示

{
"user_id": 1,
"email": "example@eample.com",
"username": "example"
}

英文:

In Rails trying to read XLSX / CSV file without fixed headers.

For example

1st CSV File, the header will be like user_id, email, dob

2nd CSV File, the header will be like user_id, username, email

So the system should manage to read them properly and should create proper json like

For the First File, JSON will be like

{
  "user_id": 1,
  "email": "example@eample.com",
  "dob": "2001-01-01",
}

For the Second File, JSON will be like

{
  "user_id": 1,
  "email": "example@eample.com",
  "username": "example"
}

答案1

得分: 1

你可以这样做:

要求 'csv'

文件1 = <<CSV1
用户ID,邮箱,出生日期
1,bob@bob.com,1998/12/1
2,alice@alice.com,2001/1/2
CSV1

文件2 = <<CSV2
用户ID,邮箱,姓名
1,bob@bob.com,bob
2,alice@alice.com,alice
CSV2

# 将行转换为哈希数组,
# 例如 [{ "用户ID" => "1", "邮箱" => "bob@bob.com", "出生日期" => "1998/12/1"},
#       { "用户ID" => "2", "邮箱" => "alice@alice.com", "出生日期" => "2001/1/2"} ]
def csv_to_array(csv_string)
  CSV.parse(csv_string, headers: true).map(&:to_h)
end

a1 = csv_to_array(文件1)
a2 = csv_to_array(文件2)

# 现在将两个数组合并,然后合并每个数组
puts a1.zip(a2).map{|a| a[0].merge(a[1])}.inspect
# => [{"用户ID" => "1", "邮箱" => "bob@bob.com", "出生日期" => "1998/12/1", "姓名" => "bob"},
#     {"用户ID" => "2", "邮箱" => "alice@alice.com", "出生日期" => "2001/1/2", "姓名" => "alice"}]
英文:

you can do it like this:

require &#39;csv&#39;

file1 = &lt;&lt;CSV1
user_id,email,dob
1,bob@bob.com,1998/12/1
2,alice@alice.com,2001/1/2
CSV1

file2 = &lt;&lt;CSV2
user_id,email,name
1,bob@bob.com,bob
2,alice@alice.com,alice
CSV2

# make an array of the rows as hashes,
# e.g. [ {&quot;user_id&quot;=&gt;&quot;1&quot;, &quot;email&quot;=&gt;&quot;bob@bob.com&quot;, &quot;dob&quot;=&gt;&quot;1998/12/1&quot;},
#        {&quot;user_id&quot;=&gt;&quot;2&quot;, &quot;email&quot;=&gt;&quot;alice@alice.com&quot;, &quot;dob&quot;=&gt;&quot;2001/1/2&quot;} ]
def csv_to_array(csv_string)
  CSV.parse(csv_string, headers: true).map(&amp;:to_h)
end

a1 = csv_to_array(file1)
a2 = csv_to_array(file2)

# now zip the two arrays, then merge each
puts a1.zip(a2).map{|a| a[0].merge(a[1])}.inspect
# =&gt; [{&quot;user_id&quot;=&gt;&quot;1&quot;, &quot;email&quot;=&gt;&quot;bob@bob.com&quot;, &quot;dob&quot;=&gt;&quot;1998/12/1&quot;, &quot;name&quot;=&gt;&quot;bob&quot;},
#     {&quot;user_id&quot;=&gt;&quot;2&quot;, &quot;email&quot;=&gt;&quot;alice@alice.com&quot;, &quot;dob&quot;=&gt;&quot;2001/1/2&quot;, &quot;name&quot;=&gt;&quot;alice&quot;}]

huangapple
  • 本文由 发表于 2023年5月29日 19:55:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定