From: William James Date: 2005-01-31T10:25:48+09:00 Subject: Re: Why csv file processing is so slow? Now assumes a quotation mark within a field is represented as "" (previous versions assumed \" ). Lacks one thing: cannot handle a newline within a field. % # Record separator. % RS = "\n" % % class Array % def to_csv % raise "Method #is_fs wasn't called." if $csv_fs.nil? % s = '' % self.map { |item| % str = item.to_s % if str.index( $csv_fs ) or /^\s|"|\s$/.match(str) % str = '"' + str.gsub( /"/, '""' ) + '"' % end % str % }.join($csv_fs) % end % def unescape % self.map!{|x| x.gsub( /""/, '"' ) } % end % end % % class String % # Set regexp for parse_csv. % # self is the field-separator, which must be % # a single character. % def is_fs % $csv_fs = self % if "^" == $csv_fs % fs = "\\^" % else % fs = $csv_fs % end % $csv_re = \ % ## Assumes embedded quotes are escaped as "". % %r! \s* % (?: % "( [^"]* (?: "" [^"]* )* )" | % ( .*? ) % ) % \s* % [#{fs}] % !x % end % def parse_csv % raise "Method #is_fs wasn't called." if $csv_re.nil? % (self+$csv_fs).scan( $csv_re ).flatten.compact.unescape % end % end % % ",".is_fs % % # Set Ruby's input record-separator. % $/ = RS % % ARGF.each_line { | line | % line.chomp! % puts line % ary = line.parse_csv % p ary % puts ary.to_csv % }