【问题标题】:CSV to JSON Ruby Script?CSV 到 JSON Ruby 脚本?
【发布时间】:2011-07-18 11:32:14
【问题描述】:

有谁知道如何编写将 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"]
]} 

【问题讨论】:

    标签: ruby json excel


    【解决方案1】:

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

     require 'csv'
     require 'json'
    
     CSV.parse(data).to_json
    

    【讨论】:

    • 谢谢不适用于 1.8,我无法升级 - 1.87 的正确语法是什么?
    • 安装 gem 'fastercsv' 并将 CSV 更改为 FasterCSV
    • data = File.open('/path/to/file.csv').read 分配数据
    【解决方案2】:

    出发地:

    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

    【讨论】:

      【解决方案3】:

      以 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
      

      【讨论】:

      • extracted_data = CSV.table('your/file.csv', header_converters: nil) 如果您想将标题保留为原始字符串,而不是骆驼化并转换为符号
      【解决方案4】:

      如果您在 Rails 项目中

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

      【讨论】:

      • 在纯 Ruby 中也能正常工作:ruby -e "require 'csv'; require 'json'" -e "puts CSV.parse(File.open(ARGV[0]), {headers: true}).map(&:to_h).to_json" example.csv
      【解决方案5】:

      我的解决方案以这种方式结束,与 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
      

      【讨论】:

        猜你喜欢
        • 2010-12-25
        • 1970-01-01
        • 2016-11-10
        • 2018-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-16
        相关资源
        最近更新 更多