Module: RIO::IF::CSV

Included in:
Rio
Defined in:
lib/rio/if/csv.rb

Instance Method Summary collapse

Instance Method Details

#columns(*ranges, &block) ⇒ Object

Select columns from a CSV file. See #csv and RIO::Doc::INTRO.



58
# File 'lib/rio/if/csv.rb', line 58

def columns(*ranges,&block) target.columns(*ranges,&block); self end

#csv(*csv_args, &block) ⇒ Object

Puts a Rio in CSV mode and sets the field and record separators. In csv mode selecting with #records will cause each line read to be parsed into a line with the CSV standard library. Specifying using #lines to select will return unparsed strings as normal.

# copy a csv file, changing the field separator
rio('afile.csv').csv > rio('afile_semicolons.csv').csv(';')

CSV mode also adds two methods #columns and #skipcolumns which allows selecting columns by column index using Fixnums or Ranges in a way similar to how lines are selected.

# iterate through every line but only get the first three columns
rio("afile.csv").csv.columns(0..2) { |array_of_fields| ... }

# iterate through every line but skip the columns 2 and 3 through 5
rio("afile.csv").csv.skipcolumns(2,3..5) { |array_of_fields| ... }

# an array containg all but the first line returning columns 5,6 and 7
rio("afile.csv").csv.columns(5..7).skiplines[0]

See RIO::Doc::INTRO for complete documentation on csv mode.



53
54
55
56
# File 'lib/rio/if/csv.rb', line 53

def csv(*csv_args,&block) 
  target.csv(*csv_args,&block); 
  self 
end

#skipcolumns(*ranges, &block) ⇒ Object

Reject columns from a CSV file. See #csv and RIO::Doc::INTRO.



60
# File 'lib/rio/if/csv.rb', line 60

def skipcolumns(*ranges,&block) target.skipcolumns(*ranges,&block); self end