Search code examples
csvlua

Lua - Remove blank lines rows from .csv file or skip them when processing?


I have a .csv file that for some reason occasionally has a blank row, which breaks my script. I’ve tried a few ways to ‘break’ out when processing, and also to re-write the whole csv without the blank rows include (as both are options I need).

Sample data in uniqueprotocsv.csv

protocol; D; S; F
RC5; 10; -1; 12
RECS80; 5; -1; 13

MCE; 4; 15; 12
NEC1; 184; -1; 157
NEC; 184; -1; 157
NECx; 44; 44; 30
NECx; 44; 44; 163

NEC1; 4; -1; 8
Akai; 1; -1; 5
Sony12; 1; -1; 21

My current attempt at ‘on the fly’ code is as follows, but it still trips up..

local file = assert(io.open("uniqueprotocsv.csv", "r")) 
    for line in file:lines() do
        if line == nil or line == "" then break
            local protocol, D, S, F = line:match("%s*(.-);%s*(.-);%s*(.-);%s*(.*)")
            local command = string.format(irptrans.. 'render --pronto --nameengine "{D='..tostring(D)..', S='..tostring(S)..', F='..tostring(F)..'}" '..tostring(protocol))
            print(command)
            local handle = assert(io.popen(command))
         end 
    handle:close()

And here’s what I’ve been working on so that I can run to remove them against the whole file, while I’m finding the unique ones in the source file. Thereby removing the need for ‘on the fly’ processing.

local csv = assert(io.open("protocsvfile.csv", "r"))
local uniquecsv = assert(io.open("uniqueprotocsv.csv", "a"))
local lines = {}
for line in csv:lines() do
 if line ~= nil or line ~= "" then
  if not lines[line] then
    --print(line)
    uniquecsv:write(line .. "\n")
  end
end
end

Neither have proved successful so any help would be appreciated     

Solution

  • You need to use and instead of or

    local csv = assert(io.open("protocsvfile.csv", "r"))
    local uniquecsv = assert(io.open("uniqueprotocsv.csv", "a"))
    local lines = {}
    for line in csv:lines() do
        if line ~= nil and line ~= "" then
            if not lines[line] then
                --print(line)
                uniquecsv:write(line .. "\n")
            end
        end
    end