Open and manipulate CSV files in Ruby

By: Vijay Emailed: 1673 times Printed: 2153 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

1. To Open a CSV formatted file for reading or writing use 

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

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.

2. Convert a line from cells data to string. Consider using CSV.generate_line instead. To generate multi-row CSV string, see EXAMPLE below.

generate_row(src, cells, out_dev, fs = nil, rs = nil)

EXAMPLE

  row1 = ['a', 'b']
  row2 = ['c', 'd']
  row3 = ['e', 'f']
  src = [row1, row2, row3]
  buf = ''
  src.each do |row|
    parsed_cells = CSV.generate_row(row, 2, buf)
    puts "Created #{ parsed_cells } cells."
  end
  p buf

ARGS

  src: an Array of String to be converted to CSV string.  Must respond to
    'size' and '[](idx)'.  src[idx] must return String.
  cells: num of cells in a line.
  out_dev: buffer for generated CSV string.  Must respond to '<<(string)'.
  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

  parsed_cells: num of converted cells.

3. parse(str_or_readable, fs = nil, rs = nil) {|row| ...} Parse lines from given string or stream. Return rows as an Array of Arrays.

# File csv.rb, line 115
  def CSV.parse(str_or_readable, fs = nil, rs = nil, &block)
    if File.exist?(str_or_readable)
      STDERR.puts("CSV.parse(filename) is deprecated." +
        "  Use CSV.open(filename, 'r') instead.")
      return open_reader(str_or_readable, 'r', fs, rs, &block)
    end
    if block
      CSV::Reader.parse(str_or_readable, fs, rs) do |row|
        yield(row)
      end
      nil
    else
      CSV::Reader.create(str_or_readable, fs, rs).collect { |row| row }
    end
  end

4. parse_line(src, fs = nil, rs = nil) Parse a line from given string. Bear in mind it parses ONE LINE. Rest of the string is ignored for example "a,b\r\nc,d" => [‘a’, ‘b’] and the second line ‘c,d’ is ignored.

If you don’t know whether a target string to parse is exactly 1 line or not, use CSV.parse_row instead of this method.

# File csv.rb, line 137
  def CSV.parse_line(src, fs = nil, rs = nil)
    fs ||= ','
    if fs.is_a?(Fixnum)
      fs = fs.chr
    end
    if !rs.nil? and rs.is_a?(Fixnum)
      rs = rs.chr
    end
    idx = 0
    res_type = :DT_COLSEP
    row = []
    begin
      while res_type == :DT_COLSEP
        res_type, idx, cell = parse_body(src, idx, fs, rs)
        row << cell
      end
    rescue IllegalFormatError
      return []
    end
    row
  end

5. parse_row(src, idx, out_dev, fs = nil, rs = nil) Parse a line from string. Consider using CSV.parse_line instead. To parse lines in CSV string, see EXAMPLE below.

EXAMPLE

  src = "a,b\r\nc,d\r\ne,f"
  idx = 0
  begin
    parsed = []
    parsed_cells, idx = CSV.parse_row(src, idx, parsed)
    puts "Parsed #{ parsed_cells } cells."
    p parsed
  end while parsed_cells > 0

ARGS

  src: a CSV data to be parsed.  Must respond '[](idx)'.
    src[](idx) must return a char. (Not a string such as 'a', but 97).
    src[](idx_out_of_bounds) must return nil.  A String satisfies this
    requirement.
  idx: index of parsing location of 'src'.  0 origin.
  out_dev: buffer for parsed cells.  Must respond '<<(aString)'.
  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

  parsed_cells: num of parsed cells.
  idx: index of next parsing location of 'src'.

Ruby Home | All Ruby Tutorials | Latest Ruby Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Vijay
What is dRuby?
Open and manipulate CSV files in Ruby
Sending Email using JSP

More Tutorials in Ruby
Standard Library Packages in Ruby
Module Abbrev in Ruby
encoding and decoding in Ruby using Base64 Module
Benchmark module in Ruby
benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) {|report| ...} in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
bmbm(width = 0) {|job| ...} in Ruby
CGI class in Ruby
URL encode a string in Ruby
escapeElement() in Ruby
escapeHTML() in Ruby
new(type = "query") in Ruby
parse(query) in Ruby
pretty() in Ruby
Format a Time object as a String in Ruby

More Latest News
Most Viewed Articles (in Ruby )
Using Proxy to connect to URLs in Ruby
URL encode a string in Ruby
URL decode a string in Ruby
Open and manipulate CSV files in Ruby
Traversing all files in a directory using Ruby
Command-line Arguments in Ruby on rails
Methods in Ruby on rails
encoding and decoding in Ruby using Base64 Module
CGI class in Ruby
escapeElement() in Ruby
Format a Time object as a String in Ruby
Reading URL content using Ruby (HTTP)
Reading emails using POP3 in Ruby
Sending emails using SMTP in Ruby
Using Forwardable in Ruby
Most Emailed Articles (in Ruby)
benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) {|report| ...} in Ruby
out() in Ruby
Sending emails using SMTP in Ruby
Using Observer in Ruby
Prompts, Command Lines, Prompts, and irb in Ruby on rails
Naming conventions in Ruby on rails
encoding and decoding in Ruby using Base64 Module
Benchmark module in Ruby
bmbm(width = 0) {|job| ...} in Ruby
escapeElement() in Ruby
Format a Time object as a String in Ruby
date and time in Ruby
Security in dRuby
Reading URL content using Ruby (HTTP)
Using Generator in Ruby