Csv檔案轉Tab檔案的代碼

來源:互聯網
上載者:User
function split(str,splitor)        if(splitor==nil) then            splitor=","    end        local strArray={}        local strStart=1        local splitorLen = string.len(splitor)        local index=string.find(str,splitor,strStart,true)        if(index==nil) then            strArray[1]=str            return strArray        end        local i=1        while index do        strArray[i]=string.sub(str,strStart,index-1)            i=i+1            strStart=index+splitorLen            index = string.find(str,splitor,strStart,true)        end        strArray[i]=string.sub(str,strStart,string.len(str))        return strArray    end            -- 去掉行首和行尾的雙引號和單引號    function sf_trim(s)        local _, quotedPart = string.match(s, "([\"'])(.-)%1")        return quotedPart    end            function ArrToTabStr(tbStrArr, tbCol)        local szOutPut = ""    local nFirstLine = 1        for _, nCol in pairs(tbCol) do        if tbStrArr[nCol] ~= nil then                if nFirstLine == 1 then                    szOutPut = szOutPut..sf_trim(tbStrArr[nCol])                    nFirstLine = 0                else                szOutPut = szOutPut..'\t'..sf_trim(tbStrArr[nCol])                end            end        end        szOutPut = szOutPut..'\n'    return szOutPut    end            --[[    本來聽取裴凱的意見,準備增加另外一個屏蔽列表的,但是這個工具只是自己用,麻煩只是一次,就不多餘操心了。    ]]    function fCsvToTabFile(szFilePath, tbSelCol)        --開啟csv檔案        local tbRet = {}        local hCsvFile = io.open(szFilePath)        if not hCsvFile then            print("打不開指定檔案:", szFilePath)            return    end                    --建立輸出的tab檔案        local hTabFile = io.open("test.txt", "w")        if not hTabFile then            print("無法建立輸出檔案")            return    end                    local nLineNum = 0        while 1 do        line = hCsvFile:read("*l")            if not line then                break        end;            local strOutPut = ArrToTabStr(split(line), tbSelCol)            if strOutPut ~= "" then                hTabFile:write(strOutPut)            end            nLineNum = nLineNum + 1        end        hCsvFile:close()        hTabFile:close()    end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.