CSV file to tab file code

Source: Internet
Author: User
Tags split
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,tr    
    UE) if (index==nil) then strarray[1]=str return Strarray end local I=1 While the index do Strarray[i]=string.sub (str,strstart,index-1) i=i+1 Strstart=index+split Orlen index = string.find (str,splitor,strstart,true) End Strarray[i]=string.sub (str,strstart,s Tring.len (str)) return Strarray end-double and single quotation mark function Sf_trim (s) Local _, Q  Uotedpart = String.match (S, "[[\"]) (.-)%1 ") Return to Quotedpart End Function Arrtotabstr (Tbstrarr, Tbcol) Local szoutput = "" Nfirstline = 1 for _, Ncol in pairs (tbcol) do if TBSTRARR[NC    
    OL] ~= Nil Then        if nfirstline = = 1 Then szoutput = Szoutput. Sf_trim (Tbstrarr[ncol]) nfirstline = 0 Else Szoutput = szoutput ... ' \ t '.. Sf_trim (Tbstrarr[ncol]) end end szoutput = Szoutput.    
\ n ' Return Szoutput end--[[would have listened to Peikai's comments, ready to add another block list, but this tool is only for their own use, the trouble is only once, don't worry about. ] Function Fcsvtotabfile (Szfilepath, Tbselcol)--open csv file local Tbret = {} Local hcsvfile = IO    
            
    . Open (Szfilepath) if not Hcsvfile then print ("Cannot open the specified file:", Szfilepath) return end --Creates the Output tab file local Htabfile = Io.open ("Test.txt", "w") if not Htabfile then PRI NT ("Cannot create output file") return end local Nlinenum = 0 while 1 does line = Hcsvfi    
        Le:read ("*l") if not line then the break end; Local STROUTPUt = arrtotabstr (split (line), Tbselcol) if Stroutput ~= "" Then Htabfile:write (stroutput) End nlinenum = nlinenum + 1 End hcsvfile:close () Htabfile:close () end

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.