Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Csv
- def self.parse(handle, args={})
- # read a csv creating a hash with the first column as the primary identifier
- # assumes that the first line of the csv file is the header
- # uses header names as keys for the values for each primary identifier
- sep = args[:sep] || ',' # record separator
- key = args[:key] || 0 # which column is the key?
- # TODO: add support for taking a Proc as an argument for key
- # data is stored as a simple hash
- data = Hash.new
- # parse header
- header = handle.gets.strip.split(sep)
- header.delete_at(key)
- # read data
- handle.each do |line|
- line = line.strip.split(sep)
- datum = Hash.new
- primary = line.delete_at key
- header.zip(line).each do |k, d|
- if datum.has_key? k
- raise "Duplicate secondary key: #{k}! in #{handle.path}"
- end
- datum[k] = d
- end
- if data.has_key? primary
- raise "Duplicate primary key: #{primary}! in #{handle.path}"
- end
- data[primary] = datum
- end
- # return data as a simple Hash
- data
- end
- end
Add Comment
Please, Sign In to add comment