From: "NAKAMURA, Hiroshi" Date: 2003-02-26T11:02:46+09:00 Subject: Re: Regexp help: Parsing a CSV file Hi, Brian, Thank you for your comment. > From: Brian Candler [mailto:B.Candler@pobox.com] > Sent: Tuesday, February 25, 2003 11:25 PM > On Tue, Feb 25, 2003 at 08:01:43PM +0900, NAKAMURA, Hiroshi wrote: > > > class String > > > def csv_to_a > > > CSV.parse_line(self) > > > end > > > end > > > > > > class Array > > > def to_csv > > > CSV.generate_line(self) > > > end > > > end > > > > parse_line and generate_line are methods of CSV2. > > CSV2 has not yet been released. > > That looks like it will be much nicer, thank you. Differences are here. http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/lib/csv/lib/csv.rb?rev=1.17&content-type=text/x-cvsweb-markup I added one-line parsing/generating methods (parse_line/generate_line) but made no change for multi-line parsing/generating. 'line' here is one record in CSV format, not one line in file. One CSV line might includes \r or \n in its fields. > What happens if the line being parsed is incomplete because > it contains a > quoted newline? In current API, class String def csv_to_a CSV::Reader.parse(self) do |row| yield row.to_a end end end $ ruby -rthis_article -e ' %Q["\n","\n"\r\n"\r","\r"].csv_to_a do |row| p row end ' => ["\n", "\n"] ["\r", "\r"] > Perhaps it could take a block to fetch any > additional lines > needed, e.g. > > while line = gets > data = CSV.parse_line(line) { gets } > end CSV::Reader.parse(ARGF) { |row| p row.to_a } ... Oops. CSV module tries to call ARGF#sysread and crashes... > In that case it would probably be better to use a block in > the first place, > i.e. > > while data = CSV.parser { gets } > # 'data' is an array containing the cells for one logical row > end Interesting. As above, current API uses block for output stream but it might be good to use block for input stream. Let me consider. > The simpler the API the better :-) Definitely. Regards, // NaHi