我的 Golang 程序会将第一个输入内容打印两次到文件中。

huangapple go评论83阅读模式
英文:

My program in Golang prints the first input two times in the file

问题

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

我的代码如下:

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	inputFile, err := os.Create("input.csv") // 创建input.csv文件
	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,有点迷茫 我的 Golang 程序会将第一个输入内容打印两次到文件中。

英文:

I try to get some CSV formatted string as input and then to print it to an actual CSV file. It works but it prints the first string 2 times.

My code looks like this:

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	inputFile, err := os.Create(&quot;input.csv&quot;) //create the input.csv file
	if err != nil {
		log.Fatal(err)
	}

	csvwriter := csv.NewWriter(inputFile)

	fmt.Println(&quot;How many records ?&quot;)
	fmt.Scanln(&amp;n)
	fmt.Println(&quot;Enter the records&quot;)
	var lines [][]string
	for i := 0; i &lt; n; i++ {
		scanner.Scan()
		text := scanner.Text()
		lines = append(lines, []string{text})
		err := csvwriter.WriteAll(lines)
		if err != nil {
			return
		}
	}
	csvwriter.Flush()
	inputFile.Close()
}

for n=2 and the records:

abcd, efgh, ijklmn
opq, rstu, vwxyz

the output looks like this:

&quot;abcd, efgh, ijklmn&quot;
&quot;abcd, efgh, ijklmn&quot;
&quot;opq, rstu, vwxyz&quot;

It is my first time working with Golang and I am a little bit lost 我的 Golang 程序会将第一个输入内容打印两次到文件中。

答案1

得分: 1

csvwriter.WriteAll(lines) WriteAll 使用 Write 将多个 CSV 记录写入 w,然后调用 Flush,返回 Flush 的任何错误。

每次在循环中读取时,您都会将行追加到文件中并刷新。

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	inputFile, err := os.Create("input.csv") // 创建 input.csv 文件
	if err != nil {
		log.Fatal(err)
	}
	defer inputFile.Close()

	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.WriteAll(lines) WriteAll writes multiple CSV records to w using Write and then calls Flush, returning any error from the Flush.

You are appending lines every time you read in a loop and flushing to the file.

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	inputFile, err := os.Create(&quot;input.csv&quot;) //create the input.csv file
	if err != nil {
		log.Fatal(err)
	}
	defer inputFile.Close()

	csvwriter := csv.NewWriter(inputFile)

	fmt.Println(&quot;How many records ?&quot;)
	fmt.Scanln(&amp;n)
	fmt.Println(&quot;Enter the records&quot;)
	var lines [][]string
	for i := 0; i &lt; n; i++ {
		scanner.Scan()
		text := scanner.Text()
		lines = append(lines, []string{text})
	}
	err = csvwriter.WriteAll(lines)
	if err != nil {
		return
	}
	
}


答案2

得分: 0

你在循环中写入csv文件,导致第一行打印了两次。以下是已更正的代码。

package main

import (
	"bufio"
	"encoding/csv"
	"fmt"
	"log"
	"os"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	inputFile, err := os.Create("input.csv") // 创建input.csv文件
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		inputFile.Close()
	}()

	csvwriter := csv.NewWriter(inputFile)
	defer func() {
		csvwriter.Flush()
	}()
	fmt.Println("有多少条记录?")
	fmt.Scanln(&n)
	fmt.Println("输入记录")
	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
	}
}

希望对你有帮助!

英文:

You were writing the csv in loop so that first line printed double. Here is the corrected code.

package main

import (
	&quot;bufio&quot;
	&quot;encoding/csv&quot;
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;os&quot;
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	inputFile, err := os.Create(&quot;input.csv&quot;) //create the input.csv file
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		inputFile.Close()
	}()

	csvwriter := csv.NewWriter(inputFile)
	defer func() {
		csvwriter.Flush()
	}()
	fmt.Println(&quot;How many records ?&quot;)
	fmt.Scanln(&amp;n)
	fmt.Println(&quot;Enter the records&quot;)
	var lines [][]string
	for i := 0; i &lt; n; i++ {
		scanner.Scan()
		text := scanner.Text()
		lines = append(lines, []string{text})

	}
	err = csvwriter.WriteAll(lines)
	if err != nil {
		return
	}
}

huangapple
  • 本文由 发表于 2022年11月26日 07:08:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/74578519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定