open(path, mode, fs = nil, rs = nil, &block) public

Open a CSV formatted file for reading or writing.

For reading.

EXAMPLE 1

  CSV.open('csvfile.csv', 'r') do |row|
    p row
  end

EXAMPLE 2

  reader = CSV.open('csvfile.csv', 'r')
  row1 = reader.shift
  row2 = reader.shift
  if row2.empty?
    p 'row2 not find.'
  end
  reader.close

ARGS

  filename: filename to parse.
  col_sep: Column separator.  ?, by default.  If you want to separate
    fields with semicolon, give ?; here.
  row_sep: Row separator.  nil by default.  nil means "\r\n or \n".  If you
    want to separate records with \r, give ?\r here.

RETURNS

  reader instance.  To get parse result, see CSV::Reader#each.

For writing.

EXAMPLE 1

  CSV.open('csvfile.csv', 'w') do |writer|
    writer << ['r1c1', 'r1c2']
    writer << ['r2c1', 'r2c2']
    writer << [nil, nil]
  end

EXAMPLE 2

  writer = CSV.open('csvfile.csv', 'w')
  writer << ['r1c1', 'r1c2'] << ['r2c1', 'r2c2'] << [nil, nil]
  writer.close

ARGS

  filename: filename to generate.
  col_sep: Column separator.  ?, by default.  If you want to separate
    fields with semicolon, give ?; here.
  row_sep: Row separator.  nil by default.  nil means "\r\n or \n".  If you
    want to separate records with \r, give ?\r here.

RETURNS

  writer instance.  See CSV::Writer#<< and CSV::Writer#add_row to know how
  to generate CSV string.
Show source
Register or log in to add new notes.