Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

go - Delete empty lines in CSV

I'm trying to clean up a CSV file that is builded like this:

;Header1;Header2;Header3;
;;;;
;1;2;3;
;;;;

How can I detect and delete those ";;;;" empty lines with Go?

Edit:

i have this so far (shortened)

r := csv.NewReader(in)
r.Comma = ';'

w := csv.NewWriter(out)
w.Comma = ';'

for {
 rec, err := r.Read()
 if err != nil {
    if err == io.EOF {
            break
        }
    return err
    }

// detect if rec is empty and go to next line (call r.Read()?)

err = w.Write(rec)

}

w.Flush()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following code should help. I've commented on the relevant parts so that it's easy to understand.

package main

import (
    "encoding/csv"
    "os"
)

// handle error using panic
func handleError(err error) {
    if err != nil {
        panic(err)
    }
}

// empty returns true if all fields are empty
func empty(record []string) bool {
    for i := range record {
        if record[i] != "" {
            return false
        }
    }
    return true
}

func main() {
    in, err := os.Open("in.csv")
    handleError(err)
    defer in.Close()

    out, err := os.Create("out.csv")
    handleError(err)
    defer out.Close()

    r := csv.NewReader(in)
    {
        r.Comma = ';'             // Delimiter
        r.FieldsPerRecord = 0     // Number of fields should match with the header
        r.TrimLeadingSpace = true // Trim the leading spaces
    }

    w := csv.NewWriter(out)
    {
        w.Comma = ';' // Delimiter
    }

    // Read all records
    records, err := r.ReadAll()
    handleError(err)

    // Write records that are not empty
    for _, record := range records {
        if !empty(record) {
            _ = w.Write(record)
        }
    }

    // Flush records in buffer
    w.Flush()
}

Input:

;Header1;Header2;Header3;
;;;;
;1;2;3;
;;;;

Output:

;Header1;Header2;Header3;
;1;2;3;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...