- 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
转载于:https://blog.51cto.com/rangercyh/1167823