使用 Ruby 解析CSV文件&YAML文件
解析CSV
图
modlesdef self.import_from_yaml(file) text = file.read text = text.gb2312_to_utf8 if !text.utf8? hash = YAML.load(text) ActiveRecord::Base.transaction do categories = _import_from_yaml_by_hash(hash) categories.each{|c|c.move_to_root} end end def self._import_from_yaml_by_hash(child_hash) return [] if child_hash.blank? child_hash.map do |parent_name,child_name_hash| categories = self._import_from_yaml_by_hash(child_name_hash) parent_category = Category.create(:name=>parent_name) categories.each{|c|c.move_to_child_of(parent_category)} parent_category end end