简体   繁体   中英

How do easily filter CSV data in Ruby

I am dealing with a CSV file(approx 500 lines). Is there a way to select data from this file with filters. I know I can do this in ruby by parsing the csv and using select/find methods But I am looking for a simpler syntax. I don't want to write methods to process each of the below queries. Any gem that would allow me do these queries? I am looking for a non-Rails solution as I am writing a plain ruby script.

eg

csv.find_rows(where: {'GENDER' => 'MALE'}.count

or

csv.find_rows(where: {'GENDER' => 'MALE', 'SALARY' >= 10000 }

I don't think you need a gem here:

csv.select { |row| row['GENDER'] == 'MALE' }
csv.select { |row| row['GENDER'] == 'MALE' || row['SALARY'] >= 10000 }

If you want activerecord syntax, the why not use activerecord itself ? Import the CSV into eg a Sqlite database, if you want to get fancy you could do it into a memory only table, a good speedup if you plan to do many queries.

require "csv"
require "sqlite3"
require "active_record"

# With activerecord
# Create our database table in to memory
ActiveRecord::Base.establish_connection(:adapter => "sqlite3",:database  => ":memory:")
unless ActiveRecord::Base.connection.table_exists?('searchengines')
  ActiveRecord::Schema.define do
    create_table :searchengines do |filename_table|
      filename_table.column :name, :string
      filename_table.column :url, :string
    end
  end
end

class Searchengine < ActiveRecord::Base
end

CSV.parse(DATA, headers: true) do |row|
  Searchengine.create({name: row[:name], url: row[:url]})
end

# or with plain sql

db = SQLite3::Database.new ":memory:"
rows = db.execute("create table searchengines (name varchar(30), url varchar(30))")

CSV.parse(DATA, headers: true) do |row|
  db.execute "insert into users values ( ?, ? )", row.fields
end

# then in activerecord

class Searchengine < ActiveRecord::Base
end

p Searchengine.where(name: "Google UK")

__END__
   Name, URL
Google UK, http://google.co.uk
Yahoo UK, http://yahoo.co.uk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM