Reading a CSV file using LUA

Source: Internet
Author: User
Tags lua split first row

New projects ready to use LUA development, static data is generally stored in the CSV, so the reference online a lot of information to write a CSV file to read the small code, because just touch lua, so just realize the function, and then optimize.

Reference xv_ly15 and the function of the stupid wood to configure their own project requirements implementation

function split (str, reps) Local resultstrslist = {}; String.gsub (str, ' [^ ': Reps..]
    + ', function (w) table.insert (Resultstrslist, W) end);
return resultstrslist;
    End--row by row fetch data local function getrowcontent (file) local content;
            Local check = False local count = 0 while true does local t = File:read () if not T and then if Count = = 0 Then check = True End Break End If not content then con Tent = T Else content = content: T end Local i = 1 while true does local index = String.find (t, "\" ", I) if not index then B 
            Reak End i = index + 1 count = count + 1 End If count% 2 = = 0 Then
    Check = True break end End If not check then assert (1~=1) end --Returns a row of data with a blank space, and a method that is not understood, and then modifies the return content and (string.gsub (content, "", "")) End FunCtion Loadcsvfile (FilePath)--read file Local alls = {} local file = Io.open (FilePath, "R") while True
    Local line = getrowcontent (file) if is not, and break end Table.insert (alls, line) end
    --[[is saved from line 2nd (the first row is the title, the next line is the content) is saved with a two-dimensional array: arr[id][property title string]] [Local titles = Split (Alls[1], ",") Local ID = 1
        Local Arrs = {} for i = 2, #alls, 1 Do--one row, the contents of each column, the first bit ID local content = Split (Alls[i], ",")
        id = tonumber (content[1])--Save the ID so that traversal is used, the original traversal can be performed using in pairs, so this does not require--table.insert (Arrs, i-1, ID) Arrs[id] = {}--with the title as the index, save the contents of each column, the value of the time to take: arrs[1]. Title for j = 1, #titles, 1 do arrs[id][titles[j]] = Content[j] end end return arrs en
 D


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.