我在 Golang 中的程序在文件中两次打印第一个输入

我尝试获取一些 CSV 格式的字符串作为输入,然后将其打印到实际的 CSV 文件中。它有效,但它打印第一个字符串 2 次。


我的代码如下所示:


func main() {

    scanner := bufio.NewScanner(os.Stdin)

    n := 0

    inputFile, err := os.Create("input.csv") //create the input.csv file

    if err != nil {

        log.Fatal(err)

    }


    csvwriter := csv.NewWriter(inputFile)


    fmt.Println("How many records ?")

    fmt.Scanln(&n)

    fmt.Println("Enter the records")

    var lines [][]string

    for i := 0; i < n; i++ {

        scanner.Scan()

        text := scanner.Text()

        lines = append(lines, []string{text})

        err := csvwriter.WriteAll(lines)

        if err != nil {

            return

        }

    }

    csvwriter.Flush()

    inputFile.Close()

}

对于 n=2 和记录:


abcd, efgh, ijklmn

opq, rstu, vwxyz

输出如下所示:


"abcd, efgh, ijklmn"

"abcd, efgh, ijklmn"

"opq, rstu, vwxyz"


这是我第一次使用 Golang,我有点迷茫 :D


阿波罗的战车
浏览 123回答 2
2回答

翻翻过去那场雪

csvwriter.WriteAll(lines) WriteAll使用 Write将多个 CSV 记录写入w,然后调用 Flush,返回来自 Flush 的任何错误。每次循环读取并刷新到文件时,您都会附加行。func main() {&nbsp; &nbsp; scanner := bufio.NewScanner(os.Stdin)&nbsp; &nbsp; n := 0&nbsp; &nbsp; inputFile, err := os.Create("input.csv") //create the input.csv file&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; defer inputFile.Close()&nbsp; &nbsp; csvwriter := csv.NewWriter(inputFile)&nbsp; &nbsp; fmt.Println("How many records ?")&nbsp; &nbsp; fmt.Scanln(&n)&nbsp; &nbsp; fmt.Println("Enter the records")&nbsp; &nbsp; var lines [][]string&nbsp; &nbsp; for i := 0; i < n; i++ {&nbsp; &nbsp; &nbsp; &nbsp; scanner.Scan()&nbsp; &nbsp; &nbsp; &nbsp; text := scanner.Text()&nbsp; &nbsp; &nbsp; &nbsp; lines = append(lines, []string{text})&nbsp; &nbsp; }&nbsp; &nbsp; err = csvwriter.WriteAll(lines)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}

江户川乱折腾

您正在循环编写 csv,以便第一行打印成双。这是更正后的代码。package mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "encoding/csv"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "os")func main() {&nbsp; &nbsp; scanner := bufio.NewScanner(os.Stdin)&nbsp; &nbsp; n := 0&nbsp; &nbsp; inputFile, err := os.Create("input.csv") //create the input.csv file&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; defer func() {&nbsp; &nbsp; &nbsp; &nbsp; inputFile.Close()&nbsp; &nbsp; }()&nbsp; &nbsp; csvwriter := csv.NewWriter(inputFile)&nbsp; &nbsp; defer func() {&nbsp; &nbsp; &nbsp; &nbsp; csvwriter.Flush()&nbsp; &nbsp; }()&nbsp; &nbsp; fmt.Println("How many records ?")&nbsp; &nbsp; fmt.Scanln(&n)&nbsp; &nbsp; fmt.Println("Enter the records")&nbsp; &nbsp; var lines [][]string&nbsp; &nbsp; for i := 0; i < n; i++ {&nbsp; &nbsp; &nbsp; &nbsp; scanner.Scan()&nbsp; &nbsp; &nbsp; &nbsp; text := scanner.Text()&nbsp; &nbsp; &nbsp; &nbsp; lines = append(lines, []string{text})&nbsp; &nbsp; }&nbsp; &nbsp; err = csvwriter.WriteAll(lines)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go