CSV 到 JSON Ruby 脚本?

发布于 2024-10-24 04:28:17 字数 644 浏览 1 评论 0原文

有谁知道如何编写将 csv 文件转换为 json 文件的 Ruby 脚本?

CSV 将采用以下格式:

Canon,Digital IXUS 70,"Epic, Epic 100",3x,Yes (lockable),Yes (lockable),Yes
Canon, Digital IXUS 75,"Epic, Epic 100",3x,Yes (lockable),Yes (lockable),Yes
Canon,Digital IXUS 80,"Epic, Epic 100",3x,Yes (lockable),Yes (lockable),Yes

JSON 将需要产生以下结果:

{ "aaData": [
[ "Canon" , "Digital IXUS 70" , "3x" , "Yes (lockable)" , "Yes (lockable)" , "Yes"],
[ "Canon" , "Digital IXUS 75" , "3x" , "Yes (lockable)" , "Yes (lockable)" , "Yes"],
[ "Canon" , "Digital IXUS 80" , "3x" , "Yes (lockable)" , "Yes (lockable)" , "Yes"]
]} 

Does anyone know how to write a Ruby script that would convert a csv file to a json file?

The CSV would be in this format:

Canon,Digital IXUS 70,"Epic, Epic 100",3x,Yes (lockable),Yes (lockable),Yes
Canon, Digital IXUS 75,"Epic, Epic 100",3x,Yes (lockable),Yes (lockable),Yes
Canon,Digital IXUS 80,"Epic, Epic 100",3x,Yes (lockable),Yes (lockable),Yes

and the JSON would need to result in this:

{ "aaData": [
[ "Canon" , "Digital IXUS 70" , "3x" , "Yes (lockable)" , "Yes (lockable)" , "Yes"],
[ "Canon" , "Digital IXUS 75" , "3x" , "Yes (lockable)" , "Yes (lockable)" , "Yes"],
[ "Canon" , "Digital IXUS 80" , "3x" , "Yes (lockable)" , "Yes (lockable)" , "Yes"]
]} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

全部不再 2024-10-31 04:28:17

这在 ruby​​ 1.9 中很容易,其中 data 是 csv 数据字符串

 require 'csv'
 require 'json'

 CSV.parse(data).to_json

This is easy in ruby 1.9 where data is your csv data string

 require 'csv'
 require 'json'

 CSV.parse(data).to_json
你与清晨阳光 2024-10-31 04:28:17

来源: 执​​行

Year,Make,Model,Description,Price
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00

此操作

[
  {:year => 1997, :make => 'Ford', :model => 'E350', :description => 'ac, abs, moon', :price => 3000.00},
  {:year => 1999, :make => 'Chevy', :model => 'Venture "Extended Edition"', :description => nil, :price => 4900.00},
  {:year => 1999, :make => 'Chevy', :model => 'Venture "Extended Edition, Very Large"', :description => nil, :price => 5000.00},
  {:year => 1996, :make => 'Jeep', :model => 'Grand Cherokee', :description => "MUST SELL!\nair, moon roof, loaded", :price => 4799.00}
]

csv = CSV.new(body, :headers => true, :header_converters => :symbol, :converters => :all)
csv.to_a.map {|row| row.to_hash }
#=> [{:year=>1997, :make=>"Ford", :model=>"E350", :description=>"ac, abs, moon", :price=>3000.0}, {:year=>1999, :make=>"Chevy", :model=>"Venture \"Extended Edition\"", :description=>"", :price=>4900.0}, {:year=>1999, :make=>"Chevy", :model=>"Venture \"Extended Edition, Very Large\"", :description=>nil, :price=>5000.0}, {:year=>1996, :make=>"Jeep", :model=>"Grand Cherokee", :description=>"MUST SELL!\nair, moon roof, loaded", :price=>4799.0}]

信用: https://technicalpickles.com/posts/parsing -csv-with-ruby

To go from:

Year,Make,Model,Description,Price
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00

To

[
  {:year => 1997, :make => 'Ford', :model => 'E350', :description => 'ac, abs, moon', :price => 3000.00},
  {:year => 1999, :make => 'Chevy', :model => 'Venture "Extended Edition"', :description => nil, :price => 4900.00},
  {:year => 1999, :make => 'Chevy', :model => 'Venture "Extended Edition, Very Large"', :description => nil, :price => 5000.00},
  {:year => 1996, :make => 'Jeep', :model => 'Grand Cherokee', :description => "MUST SELL!\nair, moon roof, loaded", :price => 4799.00}
]

Do this:

csv = CSV.new(body, :headers => true, :header_converters => :symbol, :converters => :all)
csv.to_a.map {|row| row.to_hash }
#=> [{:year=>1997, :make=>"Ford", :model=>"E350", :description=>"ac, abs, moon", :price=>3000.0}, {:year=>1999, :make=>"Chevy", :model=>"Venture \"Extended Edition\"", :description=>"", :price=>4900.0}, {:year=>1999, :make=>"Chevy", :model=>"Venture \"Extended Edition, Very Large\"", :description=>nil, :price=>5000.0}, {:year=>1996, :make=>"Jeep", :model=>"Grand Cherokee", :description=>"MUST SELL!\nair, moon roof, loaded", :price=>4799.0}]

Credit: https://technicalpickles.com/posts/parsing-csv-with-ruby

錯遇了你 2024-10-31 04:28:17

在 Josh 的示例的基础上,您现在可以使用 CSV::table

extracted_data   = CSV.table('your/file.csv')
transformed_data = extracted_data.map { |row| row.to_hash }

现在您可以调用 to_json 立即使用它,或者将其写入格式良好的文件中:

File.open('your/file.json', 'w') do |file|
  file.puts JSON.pretty_generate(transformed_data)
end

Building on Josh's example, you can now take it one step further using CSV::table:

extracted_data   = CSV.table('your/file.csv')
transformed_data = extracted_data.map { |row| row.to_hash }

Now you can call to_json to use it right away, or write it down to a file, nicely formatted:

File.open('your/file.json', 'w') do |file|
  file.puts JSON.pretty_generate(transformed_data)
end
笑,眼淚并存 2024-10-31 04:28:17

如果您在 Rails 项目中

CSV.parse(csv_string, {headers: true})
csv.map(&:to_h).to_json

If you're in a Rails project

CSV.parse(csv_string, {headers: true})
csv.map(&:to_h).to_json
若沐 2024-10-31 04:28:17

我的解决方案以这种方式结束,与 Mariu 的非常相似

require 'csv'
require 'json'

csv_table = CSV.parse(File.read('data.csv'), headers: true)
json_string = csv_table.map(&:to_h).to_json

File.open('data.json','w') do |f|
  f.puts(json_string)
end

My solution ended up this way, quite similar to Mariu's

require 'csv'
require 'json'

csv_table = CSV.parse(File.read('data.csv'), headers: true)
json_string = csv_table.map(&:to_h).to_json

File.open('data.json','w') do |f|
  f.puts(json_string)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文