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