Cocos2d-x (quick-lua) reads csv files
1. Prepare a csv file
Bytes
Ii. string segmentation Functions
function split(str, reps) local resultStrsList = {}; string.gsub(str, '[^' .. reps ..']+', function(w) table.insert(resultStrsList, w) end ); return resultStrsList;end
Iii. parsing csv Functions
Function loadCsvFile (filePath) -- reads the File local data = cc. fileUtils: getInstance (): getStringFromFile (filePath); -- divide local lineStr = split (data, '') by line ,''); -- [save from row 3rd (the first row is the title, the second row is the comment, and the following row is the content) with a two-dimensional array: arr [ID] [attribute title string] local titles = split (lineStr [1],); local ID = 1; local arrs = {}; for I = 3, # lineStr, 1 do -- the content of each column in a row: local content = split (lineStr [I],); -- use the title as an index to save the content of each column, take the value as follows: arrs [1]. title arrs [ID] ={}; for j = 1, # titles, 1 do arrs [ID] [titles [j] = content [j]; end ID = ID + 1; end return arrs; endThe parsed data is stored in the array.
Iv. Test code
local function main() local csvConfig = loadCsvFile(res/Mutou.Csv); print(csvConfig[1].Name .. : .. csvConfig[1].Des); print(csvConfig[2].Name .. : .. csvConfig[2].Des);end
In this way, the descriptions of the first and second rows of the csv file can be output. Is it much simpler than c ++. Okay, that's it!