Wednesday, June 16, 2010

Using Faster CSV to parse txt files

You can parse txt files using the following code
count = 1
    ActiveRecord::Base.connection.execute("TRUNCATE your_table")
    @parsed_file = FasterCSV.parse(params[:import][:file].read).each do |row|
      if count == 0              
        unless row[0].nil?
          data = row[0].split "\t"
          self.create(:attribute1 => data[0], :attribute2 => data[1], :attribute3 => data[2])
          table_data << data
        end
      end
      count = 0
    end

No comments:

Post a Comment