CSVParser is a library to parse and generate CSV(Comma Sepalated Value) format text data. It can do TSV(Tab Sepalated Value) format text data with tab as sepalate delimiter.
CSV format is used in many applications, however the applications have own rules. Then CSVParser refers a CSV format generated by Microsoft Excel.
Basic rules are the following:
However, fields that don't include control characters can be omitted quotes. Data outside of quotes is ignored.
ex) ^M = CR + LF
foo,bar,baz -> ["foo","bar","baz"] "foo","bar","baz" -> ["foo","bar","baz"] """foo""","bar","" -> ["\"foo\"","bar",""] " foo", bar ,baz -> [" foo"," bar ","baz"] ignore"foo","bar"ignore,"^Mbaz^M" -> ["foo","bar","\r\nbaz\r\n"]
Use install.rb or copy csvparser.rb to the directory of voluntary.
% ruby install.rb config % sudo ruby install.rb install
require "csvparser" csv = CSVParser.new_with_file "csvtext.csv"
# Displays each csv lines. csv.each do | line | p line end # Replaces "<br>" in every fields to return code. csv.each_field do | field | field.gsub!('<br>', "\n") end
CSVParser is a free software distributed under the Ruby license.
SUZUKI Tetsuya <suzuki@spice-of-life.net>
0.9.0(2003/5/8): | First release. |