| Class | CSV |
| In: |
lib/csv.rb
|
| Parent: | Object |
This program is copyrighted free software by NAKAMURA, Hiroshi. You can redistribute it and/or modify it under the same terms of Ruby‘s license; either the dual license version in 2003, or any later version.
Convert a line from cells data to string. Consider using CSV.generate_line instead. To generate multi-row CSV string, see EXAMPLE below.
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.
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.
Parse lines from given string or stream. Return rows as an Array of Arrays.
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.
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-doc.org is hosted and run by James Britt and Rising Tide Software, a Ruby application development company in Phoenix, Arizona. Ruby-doc.org was created in 2002 to promote the Ruby language and to help other Ruby hackers.
Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.
For more information on the Ruby programming language, visit ruby-lang.org.
Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.