ZhangZhihui's Blog  

 

package main

import (
    "bufio"
    "fmt"
    "io"
    "os"
)

func lineByLine(file string) error {
    f, err := os.Open(file)
    if err != nil {
        return err
    }
    defer f.Close()

    r := bufio.NewReader(f)
    for {
        line, err := r.ReadString('\n')
        if err == io.EOF {
            if len(line) != 0 {
                fmt.Println(line)
            }
            break
        }

        if err != nil {
            fmt.Printf("error reading file %s\n", err)
            return err
        }
        fmt.Print(line)
    }

    return nil
}

func main() {
    lineByLine("data.csv")
}

 

zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
Dimitris,Tsoukalos,2101112223,1600665563
Mihalis,Tsoukalos,2109416471,1600665563
Jane,Doe,0800123456,1608559903

 

package main

import (
    "bufio"
    "fmt"
    "io"
    "os"
    "regexp"
)

func wordByWord(file string) error {
    f, err := os.Open(file)
    if err != nil {
        return err
    }
    defer f.Close()

    r := bufio.NewReader(f)
    re := regexp.MustCompile(`[^\s]+`)
    for {
        line, err := r.ReadString('\n')
        if err == io.EOF {
            if len(line) != 0 {
                words := re.FindAllString(line, -1)
                for i := 0; i < len(words); i++ {
                    fmt.Println(words[i])
                }
            }
            break
        } else if err != nil {
            fmt.Printf("error reading file %s", err)
            return err
        }

        words := re.FindAllString(line, -1)
        for i := 0; i < len(words); i++ {
            fmt.Println(words[i])
        }
    }

    return nil
}

func main() {
    wordByWord("data.csv")
}

 

zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
Dimitris,Tsoukalos,2101112223,1600665563
Mihalis,Tsoukalos,2109416471,1600665563
Jane,Doe,0800123456,1608559903

 

posted on 2024-06-12 10:53  ZhangZhihuiAAA  阅读(6)  评论(0编辑  收藏  举报