第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何跳過 Go 中文件的第一行?

如何跳過 Go 中文件的第一行?

Go
胡說叔叔 2023-06-05 18:17:13
如何在 Go 中讀取文件并跳過第一行/標(biāo)題?在 Python 中我知道我能做到counter = 0with open("my_file_path", "r") as fo:    try:        next(fo)    except:        pass    for _ in fo:         counter = counter + 1這是我的 Go 應(yīng)用程序package main    import (    "bufio"    "flag"    "os")func readFile(fileLocation string) int {    fileOpen, _ := os.Open(fileLocation)    defer fileOpen.Close()    fileScanner := bufio.NewScanner(fileOpen)    counter := 0    for fileScanner.Scan() {        //fmt.Println(fileScanner.Text())        counter = counter + 1    }    return counter}func main() {    fileLocation := flag.String("file_location", "default value", "file path to count lines")    flag.Parse()    counted := readFile(*fileLocation)    println(counted)}我將讀取一個(gè)巨大的文件,如果索引為 0,我不想評(píng)估每一行。
查看完整描述

3 回答

?
www說

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

如何移動(dòng)到循環(huán)之前的下一個(gè)標(biāo)記


scanner := bufio.NewScanner(file)

scanner.Scan() // this moves to the next token

for scanner.Scan() {

    fmt.Println(scanner.Text())

}

文件


1

2

3

輸出


2

3

https://play.golang.org/p/I2w50zFdcg0


查看完整回答
反對(duì) 回復(fù) 2023-06-05
?
至尊寶的傳說

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊

例如,


package main


import (

    "bufio"

    "fmt"

    "os"

)


func readFile(filename string) (int, error) {

    f, err := os.Open(filename)

    if err != nil {

        return 0, err

    }

    defer f.Close()


    count := 0

    s := bufio.NewScanner(f)

    if s.Scan() {

        for s.Scan() {

            count++

        }

    }

    if err := s.Err(); err != nil {

        return 0, err

    }

    return count, nil

}


func main() {

    filename := `test.file`


    count, err := readFile(filename)

    if err != nil {

        fmt.Fprintln(os.Stderr, err)

        return

    }

    fmt.Println(count)

}

輸出:


$ cat test.file

1234567890

abc

$ go run count.go

1


查看完整回答
反對(duì) 回復(fù) 2023-06-05
?
jeck貓

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊

你可以嘗試這樣的事情


func readFile(fileLocation string) int {

            fileOpen, _ := os.Open(fileLocation)

            defer fileOpen.Close()

            fileScanner := bufio.NewScanner(fileOpen)

            counter := 0

            for fileScanner.Scan() {

                // read first line and ignore

                fileScanner.Text()

                break

            }


           for fileScanner.Scan() {

                // read remaining lines and process

                txt := fileScanner.Text()

                counter = counter + 1

               // do something with text

            



            return counter

        }

編輯:


func readFile(fileLocation string) int {

            fileOpen, _ := os.Open(fileLocation)

            defer fileOpen.Close()

            fileScanner := bufio.NewScanner(fileOpen)

            counter := 0

            if fileScanner.Scan() {

                // read first line and ignore

                fileScanner.Text()

            }


           for fileScanner.Scan() {

                // read remaining lines and process

                txt := fileScanner.Text()

               // do something with text

               counter = counter + 1

            }



            return counter

        }



查看完整回答
反對(duì) 回復(fù) 2023-06-05
  • 3 回答
  • 0 關(guān)注
  • 195 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)