Hello gideros people,
Could I have some help please?
I am using
@hgy29 CSV function he wrote a while ago. It is working fine for one csv file but not for two csv files.
The CSV function is:
function loadCSV(file)
local function csvsplit(line)
local t, w, l2 = {}, nil, nil
while #line > 0 do
w, l2 = line:match("\"([^\"]*)\"[,\r\n]?(.*)") -- Check for quoted string
if not w then w, line = line:match("([^,]*)[,\r\n]?(.*)") -- Non quoted
else line = l2 end
if not w then break end --Nothing or issue, break
t[#t + 1] = w
end
return t
end
local header, csv = nil, {}
for line in io.lines(file) do
f = csvsplit(line)
if not headers then -- Assume first line is headers
headers = {} for n, v in ipairs(f) do headers[v] = n end
else
csv[#csv + 1] = f
end
end
csv.getField = function(self, row, field) return self[row][headers[field]] end
return csv
end
--csv=loadCSV("135-bis-bt.csv")
--print(csv:getField(4,"Value"))
--print(csv:getField(7,"Time") |
In my case I have two csv files:
hijama _desc.csv
hijama.csv
When I use this code:
-- csv file 1
local csv_desc = loadCSV("fonts/hijama _desc.csv")
local mycsvtext1 = csv_desc:getField(3, "txtFr1")
print(mycsvtext1)
-- csv file 2 only works when csv file 1 is commented
local csv_hijama = loadCSV("fonts/hijama.csv")
local mycsvtxt2 = csv_hijama:getField(3, "front")
print(mycsvtxt2) |
the result in the console is:
2) JOURS ET MOMENTS CONSEILLES
nil
But when I comment the first part (csv file 1) then the result is correct:
abcI cannot have both csv files working at the same time to have the result:
2) JOURS ET MOMENTS CONSEILLES
abc
Do you know why is that?
Any help would be much appreciated. Thank you in advance for your time.
Comments
Likes: MoKaLux, hgy29
super cool, thank you very much for your time @pie. You are my hero of the day!
Likes: pie, antix