From: "Albert L. Wagner" Date: 2002-02-14T01:58:31+09:00 Subject: Re: (CSV) text file processing... Bob wrote: > > I am looking for an example of (csv)text file processing. What I am > specifically after is how to take a file with a CSV format and parse > it out. This will be used with MySQL to help a customer upload a file > for an inventory and then the Ruby script will parse it and place the > values into the appropriate fields in the database. > > Any such examples out there? > > Bob Here's mine: # Scan string and place into array: nilClass, String, Fixnum, Float & Date module ScanCSV require 'date' def setVariables @separator = ',' @separatorChar = @separator @quote = '"' @empty = nil @token = @empty @quoteIsOpen = false @unquotedData = false end # Answer an array of tokens found in string def arrayFromString(line) setVariables @tokens = Array.new line.each_byte {|byte|doChar(byte.chr)} doChar(@separator) @tokens end # Handle each char of scanned string def doChar(char) if char == @separator #---------------------------------------------- # Handle Separator #---------------------------------------------- if @unquotedData if @decimalPoint data = @token.to_f else if @token =~ /\s*\d+\/\s*\d+\/\d\d\d\d (\s*\d|\d\d):\d\d:\d\d/ mm, dd, yyyy = @token.split('/') data = Date.new(yyyy.to_i, mm.to_i, dd.to_i) else data = @token.to_i end end else data = @token end @tokens.push(data) @token = @empty @unquotedData = false @decimalPoint = false elsif char == @quote #---------------------------------------------- # Handle Quote #---------------------------------------------- if @quoteIsOpen @quoteIsOpen = false @separator = @separatorChar else @quoteIsOpen = true @separator = nil end else #---------------------------------------------- # Handle Other chars #---------------------------------------------- @decimalPoint = true if char == '.' and @unquotedData @token = "" if @token == @empty @token << char @unquotedData = true if not @quoteIsOpen end end end #module ScanCSV if __FILE__ == $0 include ScanCSV p arrayFromString(',,"abc",,"de,f",,123,4.56,,1/1/1990 0:00:00,') end -- "It requires wisdom to understand wisdom: the music is nothing if the audience is deaf." - Walter Lippman