Golang parsing CVS files

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. //This program is designed to automatically animate real-time effects according to the contents of CVS, the seventh column is the last value
Usage:  program name + file name
Packagemain
Import(
  "Encoding/csv"
  "Flag"
  "FMT"
  "io"
  "Log"
  "OS"
  "strings"
)
FuncMain (){
  //Parameters
  flag. Parse ()
  FMT. PRINTLN (flag. Narg ())
  ifflag. Narg ()! = 1 {
  Log. Printf ("Usage:command + filename")
  return
    }
  //Take the 1th parameter as a filename
  file, err: = OS. Open (flag. ARG (0))
  iferr! = Nil {
  FMT. Println ("Error:", err)
  return
    }
  deferfile. Close ()
  Reader: = csv. Newreader (file)
  reader.comment = ' # '//can set the annotation in the read file
  reader.comma = ', '//default is a comma, you can also set your own
  //You can also set the following information
      // Fieldsperrecord    int    //   number    of    expected    fields    per    record  
      // Lazyquotes    bool    //   allow    lazy    quotes  
      // Trailingcomma    bool    //   allow    trailing  Span style= "font-size:18px" >  comma  
  //trimleadingspace bool/  /Trim Leading  Space
  //line int             
  //column int           
  fout, err: = OS. OpenFile ("OUT.txt", OS. O_rdwr|os. O_append|os. O_create, 0666)
  iferr! = Nil {
  FMT. Println ("Error:", err)
  return
    }
  deferfout. Close ()
  Content: = ""
  k: = 0//The first row is a field name and does not require
   for{
  record, err: = Reader. Read ()
  iferr = = Io. EOF {
   break
  } else iferr! =Nil {
  FMT. Println ("Error:", err)
  return
        }
  ifk > 0 {//record is []strings, how to get the domain value directly
 for _, V: = Range Record { 
  tmp: = strings. Split (V, "|")
                //fmt. Print ("<" + Tmp[1] + ">" + Tmp[7] + "</" + Tmp[1] + ">")
  content = content + "<" + tmp[1] + ">" + tmp[7 "+" </"+ tmp[1] +" > "
            }
        }
  k = k + 1
    }
  FMT. Printf ("\ n")
  Fout. WriteString (content + "\ n")
  FMT. Printf ("\ n")
}
Related Article

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.