[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: CSV File parsing
- From: "Denis Povshedny" <denikin@...>
- Date: Thu, 20 Jul 2006 17:30:08 +0400
Hi all,
here is my 2cents. :)
==========================
local fcsv, errmsg = io.open ( "csvImport.txt", "r" )
assert( fcsv, errmsg )
local data = fcsv:read( "*a" )
fcsv:close()
-- remove spaces, todo: add tab replacement
data = string.gsub( data, " ", "" )
-- transform start/end of lines
data = "{" .. string.gsub( data, "\n", "},\n{" ) .. "}"
-- process NULLs
local nRepl = 0
repeat
data, nRepl = string.gsub( data, "{,", "{nil," )
until nRepl == 0
repeat
data, nRepl = string.gsub( data, ",}", ",nil}" )
until nRepl == 0
repeat
data, nRepl = string.gsub( data, ",,", ",nil," )
until nRepl == 0
-- wrap and run
local csv = assert( loadstring( "return {\n" .. data .. "}" ) ) ()
==========================
Multiple reassign is ineffective; the same way as loading entire file
may be not acceptable in all cases.
I used it for make code more illustrative. But for small files it works
fine, :=) see attachments for detail.
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of D Burgess
Sent: Thursday, July 20, 2006 5:01 AM
To: Lua list
Subject: Re: CSV File parsing
How do we handle non-existent values?
t = {1,,3,,5,,7,8}
I have always wished that the parser handled this.
DB
Attachment:
csvImport.zip
Description: Zip compressed data