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.
Classes and Modules
Class CSV::BasicWriterClass CSV::Cell
Class CSV::IOBuf
Class CSV::IOReader
Class CSV::IllegalFormatError
Class CSV::Reader
Class CSV::Row
Class CSV::StreamBuf
Class CSV::StringReader
Class CSV::Writer
Public Class methods
# File csv.rb, line 93
93: def CSV.foreach(path, rs = nil, &block)
94: open_reader(path, 'r', ',', rs, &block)
95: end
# File csv.rb, line 110
110: def CSV.generate(path, fs = nil, rs = nil, &block)
111: open_writer(path, 'w', fs, rs, &block)
112: end
Create a line from cells. each cell is stringified by to_s.
# File csv.rb, line 160
160: def CSV.generate_line(row, fs = nil, rs = nil)
161: if row.size == 0
162: return ''
163: end
164: fs ||= ','
165: if fs.is_a?(Fixnum)
166: fs = fs.chr
167: end
168: if !rs.nil? and rs.is_a?(Fixnum)
169: rs = rs.chr
170: end
171: res_type = :DT_COLSEP
172: result_str = ''
173: idx = 0
174: while true
175: generate_body(row[idx], result_str, fs, rs)
176: idx += 1
177: if (idx == row.size)
178: break
179: end
180: generate_separator(:DT_COLSEP, result_str, fs, rs)
181: end
182: result_str
183: end
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.
# File csv.rb, line 271
271: def CSV.generate_row(src, cells, out_dev, fs = nil, rs = nil)
272: fs ||= ','
273: if fs.is_a?(Fixnum)
274: fs = fs.chr
275: end
276: if !rs.nil? and rs.is_a?(Fixnum)
277: rs = rs.chr
278: end
279: src_size = src.size
280: if (src_size == 0)
281: if cells == 0
282: generate_separator(:DT_ROWSEP, out_dev, fs, rs)
283: end
284: return 0
285: end
286: res_type = :DT_COLSEP
287: parsed_cells = 0
288: generate_body(src[parsed_cells], out_dev, fs, rs)
289: parsed_cells += 1
290: while ((parsed_cells < cells) and (parsed_cells != src_size))
291: generate_separator(:DT_COLSEP, out_dev, fs, rs)
292: generate_body(src[parsed_cells], out_dev, fs, rs)
293: parsed_cells += 1
294: end
295: if (parsed_cells == cells)
296: generate_separator(:DT_ROWSEP, out_dev, fs, rs)
297: else
298: generate_separator(:DT_COLSEP, out_dev, fs, rs)
299: end
300: parsed_cells
301: end
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.
# File csv.rb, line 83
83: def CSV.open(path, mode, fs = nil, rs = nil, &block)
84: if mode == 'r' or mode == 'rb'
85: open_reader(path, mode, fs, rs, &block)
86: elsif mode == 'w' or mode == 'wb'
87: open_writer(path, mode, fs, rs, &block)
88: else
89: raise ArgumentError.new("'mode' must be 'r', 'rb', 'w', or 'wb'")
90: end
91: end
Parse lines from given string or stream. Return rows as an Array of Arrays.
# File csv.rb, line 115
115: def CSV.parse(str_or_readable, fs = nil, rs = nil, &block)
116: if File.exist?(str_or_readable)
117: STDERR.puts("CSV.parse(filename) is deprecated." +
118: " Use CSV.open(filename, 'r') instead.")
119: return open_reader(str_or_readable, 'r', fs, rs, &block)
120: end
121: if block
122: CSV::Reader.parse(str_or_readable, fs, rs) do |row|
123: yield(row)
124: end
125: nil
126: else
127: CSV::Reader.create(str_or_readable, fs, rs).collect { |row| row }
128: end
129: end
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
137: def CSV.parse_line(src, fs = nil, rs = nil)
138: fs ||= ','
139: if fs.is_a?(Fixnum)
140: fs = fs.chr
141: end
142: if !rs.nil? and rs.is_a?(Fixnum)
143: rs = rs.chr
144: end
145: idx = 0
146: res_type = :DT_COLSEP
147: row = []
148: begin
149: while res_type == :DT_COLSEP
150: res_type, idx, cell = parse_body(src, idx, fs, rs)
151: row << cell
152: end
153: rescue IllegalFormatError
154: return []
155: end
156: row
157: end
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'.
# File csv.rb, line 214
214: def CSV.parse_row(src, idx, out_dev, fs = nil, rs = nil)
215: fs ||= ','
216: if fs.is_a?(Fixnum)
217: fs = fs.chr
218: end
219: if !rs.nil? and rs.is_a?(Fixnum)
220: rs = rs.chr
221: end
222: idx_backup = idx
223: parsed_cells = 0
224: res_type = :DT_COLSEP
225: begin
226: while res_type != :DT_ROWSEP
227: res_type, idx, cell = parse_body(src, idx, fs, rs)
228: if res_type == :DT_EOS
229: if idx == idx_backup #((parsed_cells == 0) and cell.nil?)
230: return 0, 0
231: end
232: res_type = :DT_ROWSEP
233: end
234: parsed_cells += 1
235: out_dev << cell
236: end
237: rescue IllegalFormatError
238: return 0, 0
239: end
240: return parsed_cells, idx
241: end
# File csv.rb, line 97
97: def CSV.read(path, length = nil, offset = nil)
98: CSV.parse(IO.read(path, length, offset))
99: end